📌 A tiny library that makes working with AutoLayout easier.
A library for those who don't want to use big libraries like SnapKit, but the standard NSLayoutConstraint creation seems too verbose.
import UIKit
import Pin
class CurrencyCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(bodyView)
bodyView.addSubviews([flagLabel, titleLabel, codeLabel])
Pin.activate([
bodyView.pin.horizontally(offset: 15).vertically(),
flagLabel.pin.start().size(36).centerY(),
titleLabel.pin.after(flagLabel, offset: 15).vertically(),
codeLabel.pin.after(titleLabel, offset: 15).end().vertically()
])
}
let bodyView = UIView()
let flagLabel = UILabel()
let titleLabel = UILabel()
let codeLabel = UILabel()
}
let top = titleLabel.pin.top()
top.activate()
top.deactivate()
Activation array of constraints is more efficient than activating each constraint individually.
Pin.activate([
bodyView.pin.horizontally(offset: 15).vertically(),
flagLabel.pin.start().size(36).centerY(),
titleLabel.pin.after(flagLabel, offset: 15).vertically(),
codeLabel.pin.after(titleLabel, offset: 15).end().vertically()
])
override func viewDidLoad() {
super.viewDidLoad()
view.addSubView(toolBar)
toolBar.pin
.top(safe: true)
.horizontally()
.height(55)
.activate()
}
Add body to safe area with insets 15 pixels.
override func viewDidLoad() {
super.viewDidLoad()
view.addBody(
contentView,
safe: true,
insets: .all(15)
)
}
// Set priority for each constraint
titleLabel.pin
.start().priority(.defaultHeight)
.end().priority(.defaultLow)
// Set priority for all constraints
titleLabel.pin.start().end().priorityForAll(.defaultHeight)
let start = titleLabel.pin.start().constraints.last
start.constant = 30
start.isActive = true
You can add own extensions:
extension Pin {
public func horizontallyBetween(
_ first: UIView,
_ second: UIView,
offset: CGFloat = 0
) -> Self {
self.after(first, offset: offset)
.before(second, offset: -offset)
}
public func verticallyBetween(
_ first: UIView,
_ second: UIView,
offset: CGFloat = 0
) -> Self {
self.add(attr: .top, to: first, attr: .bottom, constant: offset)
.add(attr: .bottom, to: second, attr: .top, constant: -offset)
}
}
Pin supports rtl languages by default. If you want to force direction then use:
semanticContentAttribute = .forceLeftToRight
https://github.com/mezhevikin/Pin.git