Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS equivalent to View.GONE

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)

like image 296
scientiffic Avatar asked Sep 02 '14 15:09

scientiffic


1 Answers

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

like image 145
Linh Avatar answered Sep 30 '22 21:09

Linh