Skip to content

Instantly share code, notes, and snippets.

@xoniq
Forked from albertbori/HideableUIView.swift
Created November 21, 2015 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xoniq/49712f9647f684d212e5 to your computer and use it in GitHub Desktop.
Save xoniq/49712f9647f684d212e5 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
// Albert Bori
//
// Created by Albert Bori on 5/19/15.
// Copyright (c) 2015 Albert Bori. All rights reserved.
//
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment