IOS is equivalent to View.GONE

Is there an iOS equivalent for Android View.GONE?

On Android, setting the GONE view will make it invisible and ensure that the view does not take up space in the layout. I know with iOS, you can set the hidden view using

[viewName setHidden:true]; 

but the view still takes up space in the layout, and I think it would be inefficient to completely remove the view and recreate it later.

(note: I saw this post: the iOS equivalent for the Android View.GONE visibility mode , but there is no answer, and setting the height to 0 does not work for me, since subsequent views on the page did not shift after deleting my view)

+6
source share
4 answers

Add width / height constraints with constant = 0 to your View make your View width and height = 0 (e.g. GONE )

 // set the height constraint to 0 [self.view addConstraint:[NSLayoutConstraint constraintWithItem:theGoneView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0]]; // set the width constraint to 0 [self.view addConstraint:[NSLayoutConstraint constraintWithItem:theGoneView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0]]; 

In swift

 // set the width constraint to 0 let widthConstraint = NSLayoutConstraint(item: theGoneView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 0) view.addConstraint(widthConstraint) // set the height constraint to 0 let heightConstraint = NSLayoutConstraint(item: theGoneView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 0) view.addConstraint(heightConstraint) 

Or is it a UIView extension

 extension UIView { func goAway() { // set the width constraint to 0 let widthConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 0) superview!.addConstraint(widthConstraint) // set the height constraint to 0 let heightConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 0) superview!.addConstraint(heightConstraint) } } 

Hope for this help

+5
source

The possible equivalent could only be AFAIK:

 [yourView removeFromSuperview] 

Until you remove view from superview in ios , this will take up space in the layout.

Thus, depending on your needs, you can add or remove a view if necessary (the same as view.GONE in android).

+4
source

I am currently making the transition between Android and iOS using Swift, and this was one of my first problems. After searching the Internet, I found that some people had good results by setting the height or width of the UIView to 0, depending on whether you want the view to disappear vertically or horizontally. To implement this idea in my application, I defined two functions:

 enum Direction { case HORIZONTAL, VERTICAL } func removeView(view: UIView, direction: Direction) { // Removing the view vertically if direction == .VERTICAL { let constraint = NSLayoutConstraint(item: view as UIView, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 0) view.addConstraint(constraint) } else { // Removing the view horizontally let constraint = NSLayoutConstraint(item: view as UIView, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 0) view.addConstraint(constraint) } } // Removing the view both horizontally and vertically func removeView(view: UIView) { let constraintH = NSLayoutConstraint(item: view as UIView, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 0) let constraintW = NSLayoutConstraint(item: view as UIView, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 0) view.addConstraint(constraintH) view.addConstraint(constraintW) } 

And it looks like a simulator. Hope this helps.

+2
source

The best solution for me is to paste the components that you want to "move" into StackView (iOS 9.0+), and then by calling UIView.isHidden = true on the desired view, you definitely reach View.GONE because StackView wraps the content.

+1
source

Source: https://habr.com/ru/post/974698/


All Articles