快速书写约束的语法糖
- 此文件包含了所有的运算符的定义,可以进行基础的约束代码编写
// redView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
// <==>
redView.leadingAnchor <> view.leadingAnchor
// redView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
// <==>
redView.topAnchor <> view.topAnchor + 50
// redView.widthAnchor.constraint(equalToConstant: 100).isActive = true
// <==>
redView.widthAnchor <> 100
// redView.heightAnchor.constraint(equalTo: redView.widthAnchor).isActive = true
// <==>
redView.heightAnchor <> redView.widthAnchor
/* redButton.translatesAutoresizingMaskIntoConstraints = false
redButton.leadingAnchor.constraint(equalTo: redView.leadingAnchor).isActive = true
redButton.trailingAnchor.constraint(equalTo: redView.trailingAnchor).isActive = true
redButton.topAnchor.constraint(equalTo: redView.topAnchor).isActive = true
redButton.bottomAnchor.constraint(equalTo: redView.bottomAnchor).isActive = true */
// <==>
redButton <> redView
- 使用范例:
view.addSubview(redView)
redView.translatesAutoresizingMaskIntoConstraints = false
redView.leadingAnchor <> view.leadingAnchor
redView.topAnchor <> view.topAnchor + 50
redView.widthAnchor <> 100
redView.heightAnchor <> redView.widthAnchor
// greenLabel.leadingAnchor.constraint(greaterThanOrEqualTo: redView.trailingAnchor, constant: 10).isActive = true
// <==>
greenLabel.leadingAnchor <>> redView.trailingAnchor + 10
// redLabel.widthAnchor.constraint(lessThanOrEqualToConstant: 170).isActive = true
// <==>
redLabel.widthAnchor <<> 170
/* let constraint = redLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor)
constraint.priority = .defaultHigh
constraint.isActive = true */
// <==>
redLabel.centerYAnchor <> (view.centerYAnchor, .defaultHigh)
// or
if #available(iOS 13.0, *) {
// ⚠️⚠️⚠️以下方法仅能在iOS13及以上使用,iOS13以下机型会崩溃
let constraint = redLabel.centerYAnchor <> view.centerYAnchor
constraint.priority = .defaultHigh
}
- 此文件可以帮助两个UIView更便利的建立约束
/* yellowView.translatesAutoresizingMaskIntoConstraints = false
yellowView.topAnchor.constraint(equalTo: redView.topAnchor).isActive = true
yellowView.leadingAnchor.constraint(equalTo: redView.leadingAnchor, constant: 20).isActive = true
yellowView.heightAnchor.constraint(equalTo: redView.heightAnchor).isActive = true
yellowView.widthAnchor.constraint(equalToConstant: 30).isActive = true */
// <==>
yellowView <> (redView, [.top, .leading(20), .height, .leftWidth(30)])
/* view.addSubview(blueView)
blueView.translatesAutoresizingMaskIntoConstraints = false
blueView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
blueView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
blueView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
blueView.heightAnchor.constraint(equalToConstant: 150).isActive = true */
// <==>
view.addSubview(blueView)
blueView <> [.leading, .safeBottom, .trailing(-20), .leftHeight(150)]
// <==>
view.addSubview(blueView)
blueView <> (blueView.superview!, [.leading, .safeBottom, .trailing(-20), .leftHeight(150)])
/* blackView.translatesAutoresizingMaskIntoConstraints = false
blackView.leadingAnchor.constraint(equalTo: blueView.leadingAnchor, constant: 20).isActive = true
blackView.trailingAnchor.constraint(equalTo: blueView.trailingAnchor, constant: -20).isActive = true
blackView.topAnchor.constraint(equalTo: blueView.topAnchor, constant: 20).isActive = true
blackView.bottomAnchor.constraint(equalTo: blueView.bottomAnchor, constant: -20).isActive = true */
// <==>
blackView <> (blueView, UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20))
1.使用cocoapods
pod 'UILayoutSugar'
2.或者可以直接拷贝/LayoutSugar/LayoutSugar/Classes/
目录下的LayoutSugar.swift
和LayoutSugar+UIView.swift
文件到你的项目中,按需拷贝。