how to set visibility GONE like android in IOS? how to set visibility GONE like android in IOS? android android

how to set visibility GONE like android in IOS?


The only way to achieve Androids .GONE functionality on iOS is to use a UIStackView

via Apples documentation

Dynamically Changing the Stack View’s Content The stack view automatically updates its layout whenever views are added, removed or inserted into the arrangedSubviews array, or whenever one of the arranged subviews’s hidden property changes.

SWIFT 3:

// Appears to remove the first arranged view from the stack.// The view is still inside the stack, it's just no longer visible, and no longer contributes to the layout.let firstView = stackView.arrangedSubviews[0]firstView.hidden = true

SWIFT 4:

let firstView = stackView.arrangedSubviews[0]firstView.isHidden = true


You can achieve this easily using AutoLayout constraints.

Suppose you have three views like this:

+-----+|  A  |+-----++-----+|  B  |+-----++-----+|  C  |+-----+

and you want to make view B disappear in some cases.

Set up constraints as follows (these are just example values):

B top space to A:  4C top space to B:  4B height: 20

Then create an NSLayoutConstraint outlet in your code for B's height. Do this by dragging and dropping the constraint in IB.

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bHeight;

Finally, to make the view disappear, just do the following:

self.bHeight = 0;

Note that if you are doing this for a tableview cell, you may have cases where you want B to appear in some cells, but not in others.

In this case, you will have to reset the height to its "normal" value for those cells where you want it to be visible.

self.bHeight = 24;


I was looking for simple solution and found it. I don't have to use UIStackView or create outlet for constraint. Just use this:

class GoneConstraint {    private var constraint: NSLayoutConstraint    private let prevConstant: CGFloat    init(constraint: NSLayoutConstraint) {        self.constraint = constraint        self.prevConstant = constraint.constant    }    func revert() {        self.constraint.constant = self.prevConstant    }}fileprivate struct AssociatedKeys {    static var widthGoneConstraint: UInt8 = 0    static var heightGoneConstraint: UInt8 = 0}@IBDesignableextension UIView {    @IBInspectable    var gone: Bool {        get {            return !self.isHidden        }        set {            update(gone: newValue)        }    }    weak var widthConstraint: GoneConstraint? {        get {            return objc_getAssociatedObject(self, &AssociatedKeys.heightGoneConstraint) as? GoneConstraint        }        set(newValue) {            objc_setAssociatedObject(self, &AssociatedKeys.widthGoneConstraint, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)        }    }    weak var heightConstraint: GoneConstraint? {        get {            return objc_getAssociatedObject(self, &AssociatedKeys.heightGoneConstraint) as? GoneConstraint        }        set(newValue) {            objc_setAssociatedObject(self, &AssociatedKeys.heightGoneConstraint, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)        }    }    private func update(gone: Bool) {        isHidden = gone        if gone {            for constr in self.constraints {                if constr.firstAttribute == NSLayoutAttribute.width {                    self.widthConstraint = GoneConstraint(constraint: constr)                }                if constr.firstAttribute == NSLayoutAttribute.height {                    self.heightConstraint = GoneConstraint(constraint: constr)                }                constr.constant = 0            }        } else {            widthConstraint?.revert()            heightConstraint?.revert()        }    }}

Now, you can call view.gone = true and that's it.