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;
As my research has shown, not even AutoLayout can help you. You have to manually replace the views affected by the optionally shown component (in my case, all the views to the bottom of the optional view, but you I am sure you can adapt this to handle all the buttons to the right of your optional button):
It is advisable not to hard-code the height/width of the optional components, otherwise your code breaks every time you edit the XIB/Storyboard. I have a field float _optionalHeight which I set in viewDidLoad, so it is always up to date.
You can also clear the page, or at least certain cells of the page, and redefine the whole thing. It's fast and works well. I didn't write the code but found it in this pre-existing project I'm working on. Create
ManagementTableSection
andManagementTableCell
classes to manage it all. Sorry I can't provide better defined code.To achieve Androids.GONE functionality on iOS is to use a UIStackView. After that hide the child by position. (Apples documentation)
SWIFT 4:
It's a table cell example, just put both insides
Stack view
and get item forGONE
the child.I added a new property to a custom UIView implementation called "visible" which, when set to false, adds a constraint to collapse the view (I only added a width constraint since my list is horizontal, but the best way might be to add a height constraint of 0 as well).
You need to initialize the zero-width constraint on the view and add it as a field:
All of answers on this questions are inefficient. Best way to equvailent of Android setVisibility:Gone method on iOS is that
StackView
first select components then in editor, embed in, Stack View,connect new stack view with IBOutlet, then:
hidden:
visibility:
as using stack view, all constraints will be keeped!
Document
This question is pretty old but the closet thing I've found is setting additional constraints (so the views around the "gone" view know what to do once it's missing)
bConstraints
. Do this by:bConstraints
.@IBOutlet var bConstraints: [NSLayoutConstraint]!
in your ViewControllerThen hide B
To unhide
Obviously the more and more views you have the more complex this grows, as you need additional constraints from each view