Swift Library for Live Reloading Auto Layout Constraints on iOS
Adjust Auto Layout constraints and watch your app update immediately without a rebuild + run cycle.
Save precious time.
This library allows you to specify your auto layout constraints in separate .styles
files. It supports:
- Live reloading of Auto Layout constraints as you save the
.styles
files, without a rebuild + run cycle - Apple's Visual Format Language:
H:[p(==v2)]|
- Support an equations-based syntax:
v1.width = v2.width + 10
In addition, the .styles
files also support:
- Metrics can be specified
- Colors — rgb(), rgba() or named colors in
UIColor
, e.g.red
- Image filenames
- Button title and label text
- Font name, size
- Dynamic type style names, e.g.
callout
orcaption1
- and more
This means that you can tweak these properties without rebuilding your app during development.
WARNING: Setting of properties like colors are best used in style files only during development. Ship style files that only contains instructions for Auto Layout.
There is an example app included in the repository.
The library works best if you style based on UIViewController subclasses.
Refer to Installation to install the library
-
Create a UIViewController subclass
-
Add a property:
lazy var styler = zhiCreateStyler()
-
Override
viewDidLoad()
and create amainView
and add it to the view controller's view. You will add all other subviews tomainView
instead of the view controller's view directory:mainView.frame = view.bounds mainView.autoresizingMask = [.flexibleWidth, .flexibleHeight] view.addSubview(mainView)
-
Add the following to the end of
viewDidLoad()
(replacing the value forviews
andmetrics
accordingly):styler.views = ["celf": mainView, "v1": v1, "v2": v2] styler.metrics = ["m": 100, "k": 50] styler.keepTranslateAutoResizingMaskForView = mainView styler.liveReload(view: view) view.setNeedsUpdateConstraints()
-
Override
updateViewConstraints()
:override func updateViewConstraints() { styler.style(view: mainView) super.updateViewConstraints() }
-
In your app delegate, add (during development):
Zhi.Styler.logEnabled = true
-
Create a directory call
Styles
and create 1 style file for each view controller. If your view controller calledMyViewController
, name the fileMyViewController.styles
This is enough to get the library to style your view controller.
To get live reload to work, you need to pass in the path of the directory containing your styles to the library. The cleanest way to do this without duplication is to specify it in your Info.plist
file.
- In Xcode project settings for your target, click
Build Settings
- Click the
+
button to add aUser-Defined Setting
. - Name the setting
STYLES_DIRECTORY
- Set the value to the
Styles/
directory. eg.:$(SRCROOT)/ZhiExample/Styles/
- Delete the value for all release builds. (When this value is empty, the library will not attempt to live reload styles). See example.
- Open your
Info.plist
file (or click Info in project settings) - Add a property called
STYLES_DIRECTORY
- Set the value to:
$(STYLES_DIRECTORY)
. See example.
Styles files are line-based. Empty lines are ignored
Comments start with a single #
# This is a comment
Metrics start with ##
##m1=80
Visual Format Language:
H:|[v1][v2]-m1-|
Equation-based syntax:
v1.width = v2.width*2
Properties:
v1.backgroundColor = rgb(90, 200, 250)
i.backgroundColor = gray
i.contentMode = scaleAspectFill
i.clipsToBounds = true
i.image = color.jpeg
b.title = Tap Me
b.textStyle = largeTitle
l.fontName = Thonburi
l.fontSize = 30
l.numberOfLines = 0
Constraints especially those written in Visual Format Language are passed directly to to Auto Layout, so errors might crash the app like it normally does.
This library is currently only for iOS
.
Zhi is available through CocoaPods.
In order to install Zhi using CocoaPods, add the following line to your Podfile:
pod "Zhi"
BSD 2-clause. See LICENSE file.
Zhi is based on my previous library purplish-layout written in RubyMotion. It's also inspired by @twerth's rmq (also in RubyMotion). Some of the parsing code is shamelessly lifted from @marcoarment's CompactConstraint
- Email: hboon@motionobj.com
- Web: http://hboon.com/zhi
- Twitter: @hboon