iOS equivalent for Android View.GONE visibility mo

2019-01-21 10:24发布

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.

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.

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.

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?

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

UPDATE:

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

I tried something like this:

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

13条回答
迷人小祖宗
2楼-- · 2019-01-21 10:47
放荡不羁爱自由
3楼-- · 2019-01-21 10:47

Building off the answer provided by Deniz, here is a solution using constraints in Swift

For example: If you have 3 views, A_view B_view and C_view vertically aligned in that order and you want to "Hide" B and also adjust the difference, add a constraint

B_view.removeFromSuperView()
var constr = NSLayoutConstraint(item: C_view, 
                                attribute: NSLayoutAttribute.Top, 
                                relatedBy: NSLayoutRelation.Equal, 
                                toItem: A_view, 
                                attribute: NSLayoutAttribute.Bottom,
                                multiplier: 1,
                                constant: 20)
view.addConstraint(constr)

constant is (in this case) the amount of vertical space between C_view and A_view

查看更多
【Aperson】
4楼-- · 2019-01-21 10:51

1) If your buttons are placed vertically then you must set the height to 0 and in case of horizontally placed buttons, try setting width to 0 or you can set both to 0.

OR

2) you could try this approach which sets button2 on top of button1:

- (void)viewDidLoad
{
[super viewDidLoad];

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:@"hello" forState:UIControlStateNormal];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setTitle:@"world" forState:UIControlStateNormal];

[button1 sizeToFit]; [button2 sizeToFit];
[button2 setFrame:CGRectMake(button1.frame.origin.x, button1.frame.origin.y, button2.frame.size.width, button2.frame.size.height)];

[self.view addSubview:button1];
[self.view addSubview:button2];

}
查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-21 10:54

What you can do is to group your views under a stack view. Then when you hide a particular view, the remaining views will be shifted automatically to fill the space.

You may want to check out the Apple Documentation on Stack Views: https://developer.apple.com/reference/uikit/uistackview

or online tutorials such as: https://www.appcoda.com/stack-views-intro/

查看更多
太酷不给撩
6楼-- · 2019-01-21 10:56

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]];
查看更多
Rolldiameter
7楼-- · 2019-01-21 10:57

setHidden:TRUE/FALSE is the nearest equivalent to Android View.GONE/VISIBLE.

The View not necessarily take space if not visible!

I have made a ComboBox-Alike with a ListView laying on top of other views. I's only visible while selecting:

enter image description here

查看更多
登录 后发表回答