轉(zhuǎn)自:http://www.cnblogs.com/missingcat92/p/4686248.html?utm_source=tuicool&utm_medium=referral
自從水果發(fā)布了5代,蘋果為了適配多種屏幕尺寸,搞出了一個所謂的AutoLayout來解決問題,iOS程序員們從此走上了苦逼的適配路。
“適配”本身其實并不是一個頭疼的事情,對于4-6寸的手機來說,只要制定一套規(guī)則,就不會有太大的問題。但是令我痛苦的部分在于——iOS的 Constraints 和 VFL。
這里借用iOS開源項目 Masonry 的描述,假如我們要讓一個view在superview里居中,然后每條邊留出10像素的空白,代碼是這樣寫的:
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);[superview addConstraints:@[ //view1 constraints [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:padding.top], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:padding.left], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-padding.bottom], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeRight multiplier:1 constant:-padding.right], ]];如果你剛剛接觸iOS開發(fā),你可能認為這并不是什么問題。但是讓我們做個對比,在web開發(fā)中,應(yīng)該怎么寫呢?
#view{margin:10px;}一行代碼搞定問題,CSS是世界上最偉大的排版引擎,沒有之一。明明很簡單的事情,為什么放到iOS上,竟然變得這么這么這么復(fù)雜了?
然后蘋果又搞出了VFL,網(wǎng)上對此描述最多的是“象形文字”,雖然它真的真的改善了我們創(chuàng)建constraints的方式,讓這一切變得更快更方便,但是對于下面這段代碼,我仍然是不滿的。
constraints += NSLayoutConstraint.constraintsWithVisualFormat("V:[trackBtn]-15-[filterBtn(40)]-15-|", options: NSLayoutFormatOptions.AlignAllRight, metrics: nil, views: views)constraints += NSLayoutConstraint.constraintsWithVisualFormat("[trackBtn]-15-|", options: NSLayoutFormatOptions.AlignmentMask, metrics: nil, views: views)constraints += NSLayoutConstraint.constraintsWithVisualFormat("|-15-[filterBtn]-15-|", options: NSLayoutFormatOptions.AlignmentMask, metrics: nil, views: views)莫名其妙的options,metrics和views參數(shù)的組合,長成狗一樣的函數(shù)名。而且即使是有了VFL,我們還是離不開 constraints ,VFL并沒有真的滿足我們的所有需求,比如你看看 這個需求。
那么,我們是不是只能一聲嘆氣,說,iOS的世界就這樣了呢?
NO!!!我們有Masonry和SnapKit!!!
SnapKit是Masonry的Swift版,項目發(fā)布至今大約1年的時間,已經(jīng)在github上有兩千多個star,當(dāng)然了,這其中也少不了”他爹”Masonry給他打的那些廣告。
如果你還記得你在 StoryBoard 里面拉的那些線條,那你也一定記得其中 constriants 的表示方法:
SnapKit所做的就是這樣一件事——讓你這樣寫 constraints。
我們看看文章最頂部的那個需求用 SnapKit 如何實現(xiàn):
view1.snp_makeConstraints { (make) -> Void in make.top.equalTo(superview).offset(10) make.left.equalTo(superview).offset(10) make.bottom.equalTo(superview).offset(-10) make.right.equalTo(superview).offset(-10)}對!就是這么簡單!還可以更簡單!!
view1.snp_makeConstraints { (make) -> Void in make.edges.equalTo(superview).insets(UIEdgeInsetsMake(10, 10, 10, 10))}(注:作者只發(fā)了一張表情)
來,請跟我一起大叫三聲爽!
爽!
爽!
叫完之后我們繼續(xù)看還能怎么爽:
// 不只是相等哦,大于等于也是有的make.centerX.lessThanOrEqualTo(view2.snp_left)make.left.greaterThanOrEqualTo(label.snp_left)// 不止是邊緣哦,寬度高度也是有的// width >= 200 && width <= 400make.width.greaterThanOrEqualTo(200)make.width.lessThanOrEqualTo(400)// 其實可以簡單點,一行搞定一個sizemake.size.equalTo(CGSizeMake(50, 100))// 鏈?zhǔn)讲僮?,?yōu)先級想怎么搞就怎么搞make.left.greaterThanOrEqualTo(label.snp_left).PRiorityLow()// 媽媽再也不擔(dān)心我不會排版了,什么向左5像素向下10像素我一行代碼就搞定啦make.center.equalTo(superview).offset(CGPointMake(-5, 10))做起動畫來也是一把好手!
view1.snp_makeConstraints { (make) -> Void in self.topConstraint = make.top.equalTo(superview).offset(padding.top).constraint make.left.equalTo(superview).offset(padding.left)}// then later you can callself.topConstraint.uninstall()// or if you want to update the constraintself.topConstraint.updateOffset(5)// 也可以用 snp_updateConstraints 實現(xiàn)上述需求如果你用CocoaPods,那么Podfile你懂的:
pod 'SnapKit', :git => 'https://github.com/SnapKit/SnapKit.git', :branch => 'swift-2.0'如果你用Carthege……這個我沒用過我就不瞎說了……
總之呢,不用CocoaPods的朋友可以去 這里 下載,至于怎么弄進項目我就不管了(好好滴干嘛不用CocoaPods..)
我用SnapKit做了下面這個界面:
體驗之后發(fā)現(xiàn),其實用SnapKit和原生constraints的代碼行數(shù)差不多,但是每一行從原來的極為冗長的一坨東西,變成了很容易閱讀和維護的短小精干的代碼,代碼像下面這樣優(yōu)雅,這便是SnapKit最大的作用。
mapView.snp_makeConstraints { (make) -> Void in make.edges.equalTo(view)}locateImage.snp_makeConstraints { (make) -> Void in let topHeight = navigationController!.navigationBar.frame.height + UIapplication.sharedApplication().statusBarFrame.height make.centerX.equalTo(mapView) make.bottom.equalTo(mapView.snp_centerY).offset(topHeight/2)}filterBtn.snp_makeConstraints { (make) -> Void in make.left.equalTo(mapView).offset(15) make.right.equalTo(mapView).offset(-15) make.height.equalTo(40) make.bottom.equalTo(mapView).offset(-15)}trackBtn.snp_makeConstraints { (make) -> Void in make.bottom.equalTo(filterBtn.snp_top).offset(-15) make.right.equalTo(mapView).offset(-15)}新聞熱點
疑難解答