Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
iOS Swift Extension for easily hiding/showing UIViews constrained using Auto Layout

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.

Usage

  1. Add HideableUIView.swift to your project.
  2. Lay out and constrain your views as desired.
  3. Add failover constraints to handle the absense of the view you want to hide. (Constraints that use and are great for this.)

Example Layout

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)-|
|                   |
|-------------------|
//
// HideableUIView.swift
// Albert Bori
//
// Created by Albert Bori on 5/19/15.
// Copyright (c) 2015 Albert Bori under the MIT license.
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import UIKit
private var hideableUIViewConstraintsKey: UInt8 = 0
extension UIView {
private var _parentConstraintsReference: [AnyObject]! {
get {
return objc_getAssociatedObject(self, &hideableUIViewConstraintsKey) as? [AnyObject] ?? []
}
set {
objc_setAssociatedObject(self, &hideableUIViewConstraintsKey, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN))
}
}
func hideView() {
self.hidden = true
if let parentView = self.superview where _parentConstraintsReference.count == 0 {
//get the constraints that involve this view to re-add them when the view is shown
if let parentConstraints = parentView.constraints() as? [NSLayoutConstraint] {
for parentConstraint in parentConstraints {
if parentConstraint.firstItem === self || parentConstraint.secondItem === self {
_parentConstraintsReference.append(parentConstraint)
}
}
}
parentView.removeConstraints(_parentConstraintsReference)
}
}
func showView() {
//reapply any previously existing constraints
if let parentView = self.superview {
parentView.addConstraints(_parentConstraintsReference)
}
_parentConstraintsReference = []
self.hidden = false
}
}
@Ricardo1980

This comment has been minimized.

Copy link

commented Dec 11, 2015

Nice!
Does anybody something similar for ObjC?
Thanks.

@davidlondono

This comment has been minimized.

Copy link

commented Feb 15, 2016

doesn't work for me now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.