If you are an iOS developer and want to do something very simple, such as hide/show a UIView while using Auto Layout, you'll find that UIKit doesn't give you much help. This extension adds simple, stateful show/hide functionality to any UIView
.
- Add
HideableUIView.swift
to your project. - Lay out and constrain your views as desired.
- Add failover constraints to handle the absense of the view you want to hide. (Constraints that use
≥
and≤
are great for this.)
This is an example layout where you have a button and a label side-by-side, which are then constrained to the left side of the superview. We want pressing the [button]
to hide the [label]
. Once you get the basic constraints set up, add a trailing edge constraint to the button and attach it to the super view. Set the failover constraint value to ≥ 8
. This will make the button automatically adjust horizontally to the absense of the [label]
.
Basic Constraints
|-------------------|
| | | |
| [button]-[label]-|
| | | |
|-------------------|
Failover Constraint
|----------------|
| |
| [button]-(≥8)-|
| |
|----------------|
OR
|-------------------|
| |
| [button]-(8@999)-|
| |
|-------------------|
Nice!
Does anybody something similar for ObjC?
Thanks.