iOS equivalent to View.GONE

scientiffic picture scientiffic · Sep 2, 2014 · Viewed 7.6k times · Source

Is there an iOS equivalent to the Android View.GONE?

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

[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've seen this post: iOS equivalent for Android View.GONE visibility mode but there is no accepted answer, and setting the height to 0 did not work for me, as the subsequent views on the page did not shift after my view was removed)

Answer

Phan Van Linh picture Phan Van Linh · Mar 31, 2016

Add the width/height constraints with constant = 0 to your View will make your View have width and height = 0 (like it 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 this 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 this help