iOS 等效于 Android View.GONE 可见性模式 [英] iOS equivalent for Android View.GONE visibility mode

查看:22
本文介绍了iOS 等效于 Android View.GONE 可见性模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 iOS 开发一个应用程序,并且我正在使用启用了 AutoLayout 的 Storyboard.我的一个视图控制器有一组 4 个按钮,在某些情况下我想让第一个按钮消失.

I'm developing an app for iOS and I'm using the Storyboard with AutoLayout ON. One of my view controllers has a set of 4 buttons, and in certain circumstances i would like to make the first one disappear.

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

If I use the setHidden:TRUE method the UIButton become invisible but it still obviously take space in the view, and the result is an "hole" which I've not been able to fill making the remaining UIButton to float towards the top of the main view.

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

In Android I would have simply used View.GONE instead of View.INVISIBLE, but in iOS I'm stuck with this behaviour and I don't want to believe that the only solution is to manually (yes I mean programmatically) move the remaining elements to the top.

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

I thought I would have been able to do it setting some sort of Constraint to make everything as automatic as it is in Android but I've had no luck.

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

Before I turn Autolayout OFF, can someone point me to the right direction?

我正在使用 IB,但我也对程序化的东西感到满意.

I'm using the IB, but I'm comfortable with programmatic stuff as well.

更新:

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

Setting the component height to 0 doesn't help as well.

我尝试过这样的事情:

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

推荐答案

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

Adding a constraint(NSLayoutAttributeHeight) that sets height of the view to 0 worked for me:

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

这篇关于iOS 等效于 Android View.GONE 可见性模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆