iOS equivalent for Android View.GONE visibility mode
Solution 1
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]];
Solution 2
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:
UIView * firstView = self.svViewFontConfigure.arrangedSubviews[0];
firstView.hidden = YES;
visibility:
UIView * firstView = self.svViewFontConfigure.arrangedSubviews[0];
firstView.hidden = NO;
as using stack view, all constraints will be keeped!
Solution 3
add a height constraint to your view as follows:
then make an outlet for the height constraint in your viewController file as follows:
& then in your viewDidLoad method change constraint height to 0 as follows:
override func viewDidLoad() {
super.viewDidLoad()
nsLcButtonHeight.constant = 0
}
Solution 4
To achieve Androids.GONE functionality on iOS is to use a UIStackView. After that hide the child by position. (Apples documentation)
SWIFT 4:
let firstView = cell.rootStackView.arrangedSubviews[0]
firstView.isHidden = true // first view gone
It's a table cell example, just put both insides Stack view
and get item for GONE
the child.
Solution 5
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/
Related videos on Youtube
elbuild
Updated on July 05, 2022Comments
-
elbuild almost 2 years
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 ofView.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;
-
cahn almost 11 yearsHow about setting the height of the button to zero?
-
elbuild almost 11 yearsI tried something like this: UIButton * b; CGRect frameRect = b.frame; frameRect.size.height = 0; b.frame = frameRect; No luck :(
-
wyu over 7 yearsI know this is a super old question but regarding your update setting the frame to 0 won't help if you're using autolayout. You have to set the height constraint to 0
-
-
Nav about 9 yearsyou forgot that in android we also have view.invisible... view.gone is not similar to setHidden... its view.invisible that is similar to setHidden
-
iStar over 8 yearsSwift version:
self.view.addConstraint(NSLayoutConstraint(item: self.captchaView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 0))
-
Rick Royd Aban over 8 yearsThis should have been the answer
-
A. Petrov about 8 yearsWhat if I don't know actual height of desired view (for ex. container view rendered little bit later)?
-
Nevin Chen almost 8 yearsThanks a lot for the awesome answer! For someone may need it. I got an error
Unable to simultaneously satisfy constraints
. while I already had a constraints for the view. I solve it by stackoverflow.com/a/38625419/1270901 -
user3533716 over 7 yearsSeems like a really good solution actually, but it's worth to note that it needs iOS9+.
-
wyu over 7 yearsit would be more helpful if you explained why you couldn't implement
-
Gustavo Baiocchi Costa over 7 yearsThe second point. I am working in Xamarin though. Still haven't found a solution for this problem. Don't know how you should construct a NSLayoutConstraint array with B constraints. I assume it is done programatically, would be nice to see the code for that. Your method seems to me to be the most reasonable way of tackling this problem, however not easy to implement with just pseudo-code
-
wyu over 7 yearsI haven't used Xamarin. The way it's done in XCode is: ctrl-click and drag from the constraint to the ViewController (this is similar to how you wold create an IBOutlet in xcode). In the popup that appears (looks similar to i.stack.imgur.com/JegLK.png) change the "Connection" drop down menu from
Outlet
toOutlet Collection
. For additional constraints, ctrl drag directly to the variable name. I can upload sceenshots later today (in ~8 hours) -
Gustavo Baiocchi Costa over 7 yearsso the NSLayoutConstraint array is the outlet collection, got a bit lost here haha, tks for taking your time to answer my questions :)
-
wyu over 7 yearsyes, i'll update the answer to be more clear. So the solution is working for you?
-
Gustavo Baiocchi Costa over 7 yearsI am still trying, but finding hard to make the outlet connection in xamarin. The other problem is that I have an UIImageView with center.y connected to the right side of the B view which is the view that I need to hide
-
wyu over 7 yearsYou shouldn't need to involve subview constraints at all. Are you having difficulty selecting the constraint so you can ctrl-drag it? You can ctrl-drag from the document outline if it's easier than having to select the constraint in the interface builder - static1.squarespace.com/static/56ddcaef01dbae76cf85000d/t/…
-
Cristina De Rito almost 7 yearsI used your brilliant solution (+1 for you, thanks so much!!) but I connected via IBOutlet directly the view I wanted to show and hide, so I have to type just one line of code! :)
-
Sanjay Mangaroliya almost 7 years@iStar i have used same code but it doesn't work ...stackoverflow.com/questions/45454992/…
-
Sanjay Mangaroliya almost 7 years@Deniz i have used same code but it doesn't work ...stackoverflow.com/questions/45454992/…
-
Rajitha Perera over 6 yearsAwesome answer.Saved my day
-
emin deniz over 6 yearsThis answer actually not correct. Android View.GONE removes view on UI. For example if you set View.GONE a element, views below that wiew will shift up. So your suggestion just hides the view which is equal to View.HIDE. Look at this answer : stackoverflow.com/questions/11556607/…
-
user924 about 6 yearsbut you may have some padding, spacing for this view
-
user924 about 6 yearsconstant height sucks, I use equal height and multiplier, for example to take 25% of parent view, any example for it?
-
user924 about 6 yearsp.s. constant height sucks, I use equal height and multiplier, for example to take 25% of parent view, any example for it?
-
Ucdemir about 6 yearsI did not search on android api... But I'm mobile developer, so I know mechanic... In Android with boolean flag view can be viewable or not(Gone,Visible) If it removes from ui it will be hard to add again... but in ios,it in layout but with no spaces.... Moreover There is officialy ios hidden property which take place...UIStackview hidden property like android View.Gone
-
ZShock over 5 yearsUsually I'd use UIStackView, but this works best for cases in which you have to push your views to one side of the container view instead of distributing their spacing. Great alternative answer.