NSLayoutAnchor API是iOS9版本引入,不僅讓約束聲明更加清晰明了,而且還通過靜態類型檢查以確保約束能夠正常工作,
其實是一個工廠類,類似NSNumber這樣的設計思想.
NSLayoutAnchor用來創建NSLayoutConstraint對象,使用這些對象從而實現自動布局.
但是一般不會直接創建NSLayoutConstraint對象,而是用UIView(NSView)或者其子類,或者UILayoutGuide的某個anchor屬性(比如centerXAnchor),
這些屬性對應Auto Layout中主要的NSLayoutAttribute值(InterfaceBuilder下屬性欄可以看到),所以也可以用NSLayoutAnchor子類創建這些NSLayoutAttribute值.
個人使用之后,感覺就是語法更加易於理解和使用了,和Masonry語法一樣親切.
主意:UIView本身並沒有提供anchor屬性對應Auto Layout的margin屬性,但是UILayoutGuide有這樣的屬性與之對應.
1.使用NSLayoutConstraint創建
constraints,實現一個100*100大小,左邊和superView相距20,頂部相距100的View
2.使用LayoutAnchor實現類似的約束
對比可以看到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可以查閱文檔,更深入了解,實現自己想要的效果