1. Requirements
- iOS 9.0+
- Xcode 10.0+
- Swift 4.2+
2. Installation
1.CocoaPods
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
CocoaPods 1.1.0+ is required to build DCAutoLayout 1.0.0+.
To integrate DCAutoLayout into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'DCAutoLayout', '~> 1.0.1'
end
Then, run the following command:
$ pod install
2.Carthage
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate DCAutoLayout into your Xcode project using Carthage, specify it in your Cartfile
:
github "DarielChen/DCAutoLayout"
Run carthage update
to build the framework and drag the built DCAutoLayout.framework
into your Xcode project.
3.Manually
If you prefer not to use either of the aforementioned dependency managers, you can integrate SnapKit into your project manually.
3.Usage
1.Quick Start
import DCAutoLayout
class ViewController: UIViewController {
lazy var label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
label.backgroundColor = UIColor.groupTableViewBackground
view.addSubview(label)
label.layout {
$0.topAnchor == view.safeAreaTopAnchor + 20
$0.leadingAnchor == view.leadingAnchor + 20
$0.trailingAnchor == view.trailingAnchor - 20
$0.heightAnchor == 44
}
}
}
2.Set superview’s edges with 20pts of padding.
label.layout {
$0.topAnchor == view.topAnchor + 20
$0.leadingAnchor == view.leadingAnchor + 20
$0.trailingAnchor == view.trailingAnchor - 20
$0.bottomAnchor == view.bottomAnchor - 20
}
Or even shorter:
label.layout {
$0 == view.marign(20, 20, 20, 20)
}
>=
、<=
represent greaterThanOrEqual
、lessThanOrEqual
.
3.You can use >=
or <=
to change Anchor.
label.layout {
$0.centerAnchor == view.centerAnchor
$0.widthAnchor == view.bounds.width - 40
$0.heightAnchor >= 44
}
set height to 88pts.
label.layout {
$0.heightAnchor == 88
}
4.Composition
marign
label.layout {
$0 == view.marign(20, 20, 20, 20)
}
size
label.layout {
$0.sizeAnchor == view.size(100, 44)
}
center
label.layout {
$0.centerAnchor == view.centerAnchor
}
5.updateConstraints
>=
、<=
to updateConstraints.
Use label.layout {
$0.heightAnchor >= 44
}
Update constraint.
label.layout {
$0.heightAnchor == 88
}
reset anchor
Set heightAnchor after remove heightAnchor.
label.layout {
$0.removeAnchor($0.heightAnchor)
$0.heightAnchor == 88
}
clear all anchor
Set any anchor after remove all anchor.
label.removeAllAnchor()
label.layout {
$0.removeAnchor($0.heightAnchor)
$0.heightAnchor == 88
}
4.License
DCAutoLayout is released under the MIT license. See LICENSE for details.