Skip to content

Instantly share code, notes, and snippets.

@Automatt
Forked from albertbori/HideableUIView.swift
Last active January 4, 2018 07:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Automatt/e25288d5bcc618e17449 to your computer and use it in GitHub Desktop.
Save Automatt/e25288d5bcc618e17449 to your computer and use it in GitHub Desktop.
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)-|
|                |
|----------------|
//
// HideableUIView.swift
// Jarvis
//
// Created by Mathew Spolin on 9/22/15.
// Copyright © 2015 AppDirect. All rights reserved.
//
import Foundation
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 as! [NSLayoutConstraint])
}
}
func showView() {
//reapply any previously existing constraints
if let parentView = self.superview {
parentView.addConstraints(_parentConstraintsReference as! [NSLayoutConstraint])
}
_parentConstraintsReference = []
self.hidden = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment