iphone - iOS 相当于 Android View.GONE 可见性模式

标签 iphone ios interface-builder visibility autolayout

我正在为 iOS 开发一个应用程序,我正在使用 Storyboard 和 AutoLayout ON。我的一个 View Controller 有一组 4 个按钮,在某些情况下我想让第一个按钮消失。

如果我使用 setHidden:TRUE 方法,UIButton 会变得不可见,但它显然仍会占用 View 中的空间,结果是一个我无法填充的“洞”剩余的 UIButton 漂浮到主视图的顶部。

在 Android 中,我会简单地使用 View.GONE 而不是 View.INVISIBLE,但在 iOS 中我坚持这种行为,我不想相信唯一的解决方案是手动(是的,我的意思是以编程方式)将剩余元素移动到顶部。

我以为我可以通过设置某种约束来使一切都像在 Android 中一样自动,但我没有运气。

在我关闭自动布局之前,有人可以指出正确的方向吗?

我正在使用 IB,但我也很喜欢程序化的东西。

更新:

将组件高度设置为 0 也无济于事。

我试过这样的:

UIButton *b;
CGRect frameRect = b.frame;
frameRect.size.height = 0;
b.frame = frameRect;

最佳答案

添加一个将 View 高度设置为 0 的约束(NSLayoutAttributeHeight)对我有用:

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.captchaView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0]];

关于iphone - iOS 相当于 Android View.GONE 可见性模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17869268/

相关文章:

ios - 关联 View 消失后调用的 CATiledLayer drawInContext

ios - 为什么在 VIPER 架构中双向使用协议(protocol)而不是单向使用协议(protocol)

cocoa-touch - Interface Builder 中提供 "Mode"、 "Scale to fill"等的 "Aspect fit"属性是什么?

ios - UILabel 滚动后不遵守约束(xib 内的标签,作为 uitableviewcell 排队)

ios - 快速与容器 View 交互

iphone - 发现 iOS 二进制文件的 iOS 版本

iphone - 中断后声音丢失

iphone - AVAudioSession sharedInstance requestRecordPermission :^(BOOL granted)

iphone - ios - 如何在字符串中动态调用另一个类的方法?

objective-c - IOS:字符串强调和粗体