AnchorLayoutKit is a simple layout tool for iOS.
Ever wonder how to increase productivity while building iOS layout in code? Bored of typing repetitive code with 5 lines just to put a UIView on view?
Look no further because AnchorLayoutKit will help you to improve productivity to build world-scale applications.
Top 5 App Store (USA) application using this kit:
New spin off application from Recolor also builded with this kit:
Personal live applications on App Store builded with this kit:
- Boxline, Download
- FDMRI, Manual dexterity exam for enrollment at the Faculty of Dental Medicine of Rijeka. Download. Req ID for entering app.
- iOS 10+
- Xcode 11+
- Swift 5
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but AnchorLayoutKit does support its use on supported platforms.
Once you have your Swift package set up, adding AnchorLayoutKit as a dependency is as easy as adding it to the dependencies value of your Package.swift.
dependencies: [
.package(url: "https://github.com/IvicaPetrsoric/AnchorLayoutKit", .upToNextMajor(from: "1.0.6"))
]
Add simple red UIView element on the view, defining anchors, size and padding
override func viewDidLoad() {
super.viewDidLoad()
// setup
let redView = UIView()
redView.backgroundColor = .red
view.addSubview(redView)
// implementation
}
- Currently with Swift
// add redView to fill whole view
redView.translatesAutoresizingMaskIntoConstraints = false
redView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
redView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
redView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
redView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
- With AnchorLayoutKit
-
- A) Anchor to whole supper view
Implementation | Result |
---|---|
Add redView to fill whole view | ![]() |
|
-
- B) Anchor to whole supper view
// add redView to fill whole view
redView.anchorFillSuperview()
-
- C) Anchor to whole supper view but taking safe area in account, also adding some padding to leading and trailing anchor
Implementation | Result |
---|---|
Add redView to fill whole view (safe area included) with padding left and right | ![]() |
|
- Adding view depending on other view
-
- A) Depending on self.view
// Add redView to fill whole view but taking safe area in account
redView.anchor(top: view.topAnchor, leading: view.leadingAnchor,
bottom: view.bottomAnchor, trailing: view.trailingAnchor)
-
- B) Depending on other view, like red View
-
- C) Depending on other view, like red View but with centering on X axis (super view)
-
- D) Depending on other view, like red View but with centering on Y axis (super view), padding is on trailing with positive padding
-
- E) Depending on other view, like red View but with centering on X axis (red view), padding is on top anchor with positive padding
-
- F) Depending on other view, like red View but with centering on X axis (red view) with padding to the right on x axis, padding is on top anchor with positive padding
- Centering View
-
- A) With secific size
Implementation | Result |
---|---|
Set size to 40x40 | ![]() |
|
-
- B) With secific size, and padding to the right and bit down
Implementation | Result |
---|---|
Set size to 88x88, centered and padding on X and Y from center | ![]() |
|
- Setting specific sizes
-
- A) Setting with/hegith/both as constants
// setup
blueView.anchor(top: redView.bottomAnchor, leading: view.leadingAnchor, bottom: nil, trailing: nil,
padding: .init(top: 60, left: 60, bottom: 0, right: 0))
// setup width
blueView.anchorConstraintWidth(constant: 80)
// setup height
blueView.anchorConstraintHeight(constant: 80)
// setup width and height
blueView.anchorConstraintSize(constantX: 80, constantY: 80)
-
- B) Setting width/height depending on a specific widht/height anchor of view
- Remove ALL applied constraints to specific view
blueView.anchorRemoveConstraints()
- Extra
-
- A) Enable/Disable activate state of constraint
-
- B) Add multiple views in one line
view.addSubviews(redView, blueView, yellowView, greenView)
-
- C) KeyWindow
// get keywindow as optional (old ways UIApplication.shared.keyWindow)
if let keyWindow = UIWindow.keyWindow {
// do stuff
}
-
- D) out of the box you can get safeTopAnchor which represents top anchor which take in account safe area layout guide. Also the other anchor can be returned with safe area in account
// safe area top anchor
view.safeTopAnchor
// safe area leadnig anchor
view.safeLeadingAnchor
// safe area bottom anchor
view.safeBottomAnchor
// safe area trailing anchor
view.safeTrailingAnchor
- Animations - If want to animate a specific anchor, don't worry, there is an easy way in this kit, just use the anchor method anf pick return anchor you want and update it. In the following expample top anchor is animated
// setup
// picked top anchor as return, can be used self so a return,
// self represents AnchoredConstraints struct with top, leading, bottom, trailing,
// width, height anchor properties, all of those properties can be manipulated/animated
let blueViewtopAnchor = blueView.anchor(top: view.topAnchor, leading: view.leadingAnchor,
bottom: nil, trailing: nil,
padding: .init(top: 0, left: 0, bottom: 0, right: 0),
size: .init(width: 100, height: 100)).top
self.view.layoutIfNeeded()
// moves the bluew view down of the top anchor for 100 px after one sec and with duration of 5 sec
UIView.animate(withDuration: 5, delay: 1) {
blueViewtopAnchor?.constant = 100
self.view.layoutIfNeeded()
}