NSLayoutAnchor API是iOS9版本引入,不只让约束声明更加清晰明了,并且还经过静态类型检查以确保约束可以正常工做,swift
实际上是一个工厂类,相似NSNumber这样的设计思想.app
NSLayoutAnchor用来建立NSLayoutConstraint对象,使用这些对象从而实现自动布局.ide
可是通常不会直接建立NSLayoutConstraint对象,而是用UIView(NSView)或者其子类,或者UILayoutGuide的某个anchor属性(好比centerXAnchor),布局
这些属性对应Auto Layout中主要的NSLayoutAttribute值(InterfaceBuilder下属性栏能够看到),因此也能够用NSLayoutAnchor子类建立这些NSLayoutAttribute值.动画
我的使用以后,感受就是语法更加易于理解和使用了,和Masonry语法同样亲切.ui
主意:UIView自己并无提供anchor属性对应Auto Layout的margin属性,可是UILayoutGuide有这样的属性与之对应.spa
1.使用NSLayoutConstraint建立设计
constraints,实现一个100*100大小,左边和superView相距20,顶部相距100的View对象
2.使用LayoutAnchor实现相似的约束blog
对比能够看到NSLayoutAnchor类提供了更有优点的NSLayoutConstaint API
:1.更加整洁,优雅,易读
2.经过NSLayoutAnchor中的方法来约束锚点参数以及做为接收器的相同泛型类型(NSLayoutAttribute),API 便可以使用类型检查以确保可以建立出有效的约束
主意:尽管NSLayoutAnchor类会进行类型检测,但然不能必定确保建立的约束是有效的.好比一个View的leadingAnchor和另一个View的leftAnchor,进行约束,尽管都是NSLayoutXAnchor的实例,编译也能经过,可是,Auto Layout不容许leading和trailling的属性和left或者right混和约束.会致使在运行时崩溃的结果.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: A constraint cannot be made between a leading/trailing attribute and a right/left attribute. Use leading/trailing for both or neither.'
*** First throw call stack:
*/
实现的效果
3.NSLayoutAnchor的练习
实现2个view大小同样,和屏幕距离为20,
let yellowView = UIView() yellowView.backgroundColor = UIColor.yellowColor() yellowView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(yellowView) let blueView = UIView() blueView.backgroundColor = UIColor.blueColor() blueView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(blueView) //LayoutAnchor约束
let yLeftCon = yellowView.leftAnchor.constraintEqualToAnchor(view.leftAnchor, constant: 20) let ytopCon = yellowView.topAnchor.constraintEqualToAnchor(view.topAnchor, constant: 250) let yHeightCon = yellowView.heightAnchor.constraintEqualToConstant(150) let yWidthCon = yellowView.widthAnchor.constraintEqualToAnchor(blueView.widthAnchor) let yRightCon = yellowView.rightAnchor.constraintEqualToAnchor(blueView.leftAnchor,constant: -100) let bLeftCon = blueView.leftAnchor.constraintEqualToAnchor(yellowView.rightAnchor, constant: 100) let bTopCon = blueView.topAnchor.constraintEqualToAnchor(yellowView.topAnchor) let bRightCon = blueView.rightAnchor.constraintEqualToAnchor(view.rightAnchor, constant: -20) let bWidthCon = blueView.widthAnchor.constraintEqualToAnchor(yellowView.widthAnchor) let bHeightCon = blueView.heightAnchor.constraintEqualToAnchor(yellowView.heightAnchor) NSLayoutConstraint.activateConstraints([yLeftCon,ytopCon,yHeightCon,yWidthCon,yRightCon,bLeftCon,bTopCon,bRightCon,bWidthCon,bHeightCon])
最终的效果
4.Layout动画的实现
let conX = iconView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor) conY = iconView.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor) let conW = iconView.widthAnchor.constraintEqualToConstant(100.0) let conH = iconView.heightAnchor.constraintEqualToConstant(100.0) NSLayoutConstraint.activateConstraints([conX,conY,conW,conH]) override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) UIView.animateWithDuration(1.0, delay: 2.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.0, options: [], animations: { () -> Void in self.conY.constant -= 100 self.view.layoutIfNeeded() }, completion: nil) }
会看到进入界面2秒后,iconView在Y方向上有一个位移弹性动画
关于更多的NSLayout Anchor能够查阅文档,更深刻了解,实现本身想要的效果