IOS equivalent for Android View.GONE visibility mode

I am developing an application for iOS and I am using Storyboard with AutoLayout ON. One of my view controllers has a set of 4 buttons, and in some cases I would like the first one to disappear.

If I use the setHidden:TRUE method, the UIButton becomes invisible, but it still clearly takes up space in the view, and the result is a β€œhole” that I could not fill so that the remaining UIButton floated in the top direction of the main view.

In Android, I would just use View.GONE instead of View.INVISIBLE , but on iOS I am stuck with this behavior and I don’t want to believe that the only solution is to manually (yes, I mean programmatically) move the remaining elements to the beginning.

I thought I could do this by installing some kind of Constraint to make everything as automatic as on Android, but I had no luck.

Before disabling auto-shutdown, can someone point me in the right direction?

I use IB, but I also like the program material.

UPDATE:

Setting the height of the component to 0 also does not help.

I tried something like this:

 UIButton *b; CGRect frameRect = b.frame; frameRect.size.height = 0; b.frame = frameRect; 
+54
visibility ios iphone autolayout interface-builder
Jul 25 '13 at 21:41
source share
12 answers

Adding a constraint (NSLayoutAttributeHeight) that sets my view height to 0:

 [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.captchaView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0]]; 
+29
Mar 26 '14 at 19:06
source share

add a height constraint to your view as follows: enter image description here

then create an output to limit the height in the viewController file as follows: enter image description here

& then in your viewDidLoad method change the height of the constraint to 0 as follows:

 override func viewDidLoad() { super.viewDidLoad() nsLcButtonHeight.constant = 0 } 
+11
Jan 20 '16 at 16:54 on
source share

All answers to these questions are ineffective. The best way for equvailent Android setVisibility: the Gone method in iOS is that StackView select components first, and then in the editor, implement Stack View,

connect a new stack view using IBOutlet, then:

hidden:

 UIView * firstView = self.svViewFontConfigure.arrangedSubviews[0]; firstView.hidden = YES; 

visibility:

 UIView * firstView = self.svViewFontConfigure.arrangedSubviews[0]; firstView.hidden = NO; 

like using a stack view, all restrictions will be saved!

Document

+9
Sep 25 '16 at 12:47
source share

What you can do is group your views in a stack. Then, when you hide a specific view, the rest of the views will be automatically shifted to fill the gap.

You might want to check out Apple's documentation on Stack Views: https://developer.apple.com/reference/uikit/uistackview

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

+3
Feb 05 '17 at 5:03 on
source share

1) If your buttons are arranged vertically, you should set the height to 0, and in the case of horizontally arranged buttons, try setting the width to 0 or you can set both values ​​to 0.

OR

2) you can 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]; } 
+1
Jul 26 '13 at 9:14
source share
+1
May 13, '14 at 15:33
source share

As my research has shown, even AutoLayout cannot help you. You must manually replace the views affected by the optionally displayed component (in my case, all the views are below for an additional view, but I'm sure you can adapt them to handle all the buttons to the right of your additional button):

 - (IBAction)toggleOptionalView:(id)sender { if (!_expanded) { self.optionalView.frame = CGRectMake(self.optionalView.frame.origin.x, self.optionalView.frame.origin.y, self.optionalView.frame.size.width, _optionalHeight); self.bottomView.frame = CGRectMake(self.bottomView.frame.origin.x, self.bottomView.frame.origin.y+_optionalHeight, self.bottomView.frame.size.width, self.bottomView.frame.size.height); _expanded = YES; } else { self.optionalView.frame = CGRectMake(self.optionalView.frame.origin.x, self.optionalView.frame.origin.y, self.optionalView.frame.size.width, 0); self.bottomView.frame = CGRectMake(self.bottomView.frame.origin.x, self.bottomView.frame.origin.y-_optionalHeight, self.bottomView.frame.size.width, self.bottomView.frame.size.height); _expanded = NO; } } 

It is advisable not to hardcode the height / width of additional components, otherwise your code will be interrupted every time you edit XIB / Storyboard. I have a float _optionalHeight field that I set to viewDidLoad, so it is always updated.

0
Mar 14 '14 at 10:05
source share

You can also clear the page, or at least certain cells in the page, and override it all. It works fast and well. I did not write the code, but found it in this already existing project that I am working on. Create the ManagementTableSection and ManagementTableCell classes to manage all this. Sorry, I cannot provide a clearer code.

0
Sep 17 '14 at 14:38
source share

Based on the answer provided by Deniz, here is a solution using restrictions in Swift

For example: if you have 3 views, A_view B_view and C_view are vertically aligned in that order, and you want to β€œHide” B, and also adjust the difference, add a limit

 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 (in this case) the amount of vertical space between C_view and A_view

0
Feb 25 '15 at 4:30
source share

I added a new property to a custom UIView implementation with the name "visible", which when set to false adds a constraint to collapse the view (I added only the width constraint, since my list is horizontal, but the best way might be to add a height constraint of 0).

 var visible:Bool = true{ didSet{ if(visible){ clipsToBounds = false; removeConstraint(hideConstraint!) }else{ clipsToBounds = true addConstraint(hideConstraint!) } } } 

You need to initialize the zero-width constraint in the view and add it as a field:

 private var hideConstraint:NSLayoutConstraint? func someInitFunction(){ hideConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 0.0) ... } 
0
Feb 25 '15 at 13:32
source share

This question is quite old, but I found that additional restrictions were set in the closet (so that the views around the "departed" view know what to do when it is absent).

 A which you want to be A | after setting B to gone | BC | C 
  • Set a lower priority constraint (750) from C to A.
  • Add B upper and lower constraints (either left and right if you want your view to collapse horizontally) in the NSLayoutConstraint bConstraints . Do it:
    • Control click and drag from view constraint on ViewController
    • Change connection from Outlet to Outlet Collection
    • For the name bConstraints .
    • Hit will connect. This will create @IBOutlet var bConstraints: [NSLayoutConstraint]! in your ViewController
    • To add additional restrictions: drag from the restriction in the Storyboard to the variable name @IBOutlet
  • Then hide B

     B.hidden = true NSLayoutConstraint.deactivateConstraints(bConstraints) 
  • To display

     B.hidden = false NSLayoutConstraint.activateConstraints(bConstraints) 

Obviously, more and more views you have more complicated, as you need additional restrictions from each view

0
Jan 06 '17 at 21:59
source share

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

Viewing does not necessarily take up space if it is not visible!

I created a ComboBox-Alike with a ListView on top of other views. I see only when choosing:

enter image description here

-2
Oct 11 '14 at 10:32
source share



All Articles