Skip to content

A tiny extension for UIView for disabling and enabling layout constraints easier

Notifications You must be signed in to change notification settings

DagAgren/DisableConstraints

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

DisableConstraints

This is an extension for the UIView class in Swift to make it easier to temporarily disable and enable layout constraints for a certain view. Especially in UITableView and UICollectionView cells, it is often useful to hide certain UI elements and show them again when the cell is reused. However, layout constraints remain active while the view is hidden, which is often not desired.

However, keeping track of all constraints is annoying, and disabling constraints without storing references to them makes them deallocate. This extension will keep your disabled constraints in safe storage until its time to enable them again.

For example, consider the case where we have three views, A, B and C, with spacing constraints between each:

[ A ] -- [ B ] -- [ C ]

If you hide B, then there will still be an empty space where it was, as its constraints are still active. Setting up new constraints programmatically is a lot of work. Instead, you can set up a lower-priority constraint between A and C, then hide B and disable its constraints. The lower-priority constraint will now space A and C correctly, and once B is shown and its constraints are re-enabled, the layout will return to what ir originally was.

Installation

Just download and copy DisableConstraints.swift into your project. It's just a single file.

Usage

// Hide view B and disable all constraints related to it.
viewB.isHidden = true
viewB.areConstraintsActive = false
...
// Show view B again and re-enable all constraints related to it.
viewB.isHidden = false
viewB.areConstraintsActive = true

There are also two simple helper functions to make your code a bit clearer and get rid of the mess of trues and falses:

// Hide view B and disable all constraints related to it.
viewB.hideAndDisableConstraints()
...
// Show view B again and re-enable all constraints related to it.
viewB.showAndEnableConstraints()

Author

Dag Ågren ([email protected], @[email protected], @WAHa_06x36, wakaba.c3.cx)

License

This code is released into the public domain with no warranties. If that is not suitable, it is also available under the CC0 license.

About

A tiny extension for UIView for disabling and enabling layout constraints easier

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages