xcode 6使用storyboard 進行自動佈局,迷惑的問題主要由:ios
1,classsize 究竟是一個什麼東東?git
2,classSize 和 layout 有什麼區別?github
3, 如何使用storyboard 進行自動佈局xcode
4,什麼是約束?app
5,常見的約束報錯有哪些?oop
6,在開發過程當中(使用storyboard)應該注意哪些問題?佈局
這些問題我會在ui
代碼級別的界面顯示 spa
1,若是是從代碼層面開始使用Autolayout,須要對使用的View的translatesAutoresizingMaskIntoConstraints的屬性設置爲NO.
便可開始經過代碼添加Constraint,不然View仍是會按照以往的autoresizingMask進行計算.
而在Interface Builder中勾選了Ues Autolayout,IB生成的控件的translatesAutoresizingMaskIntoConstraints屬性都會被默認設置NO..net
2,值得注意的是,添加約束以前必定要將子視圖優先addSubview到父視圖中,不然在添加約束時會產生編譯器警告.
而咱們在理解的時候,能夠經過這種方式來理解.item1.attribute = multiplier ⨉ item2.attribute + constant,好比看下面的代碼
- UIView *newView = [[UIView alloc]init];
- newView.backgroundColor = [UIColor redColor];
-
- [self.view addSubview:newView];
-
-
- newView.translatesAutoresizingMaskIntoConstraints =NO;
-
- NSLayoutConstraint *constraint = nil;
-
- constraint = [NSLayoutConstraint constraintWithItem:newView
- attribute:NSLayoutAttributeLeading
- relatedBy:NSLayoutRelationEqual
- toItem:self.view
- attribute:NSLayoutAttributeLeading
- multiplier:1.0f
- constant:20];
- [self.view addConstraint:constraint];
-
- constraint = [NSLayoutConstraint constraintWithItem:newView
- attribute:NSLayoutAttributeTrailing
- relatedBy:NSLayoutRelationEqual
- toItem:self.view
- attribute:NSLayoutAttributeTrailing
- multiplier:1.0f
- constant:-20];
- [self.view addConstraint:constraint];
-
- constraint = [NSLayoutConstraint constraintWithItem:newView
- attribute:NSLayoutAttributeTop
- relatedBy:NSLayoutRelationEqual
- toItem:self.view
- attribute:NSLayoutAttributeTop
- multiplier:1.0f
- constant:80];
- [self.view addConstraint:constraint];
-
- constraint = [NSLayoutConstraint constraintWithItem:newView
- attribute:NSLayoutAttributeBottom
- relatedBy:NSLayoutRelationEqual
- toItem:self.view
- attribute:NSLayoutAttributeBottom
- multiplier:1.0f
- constant:-20];
- [self.view addConstraint:constraint];
建立了一個單視圖,距離各邊界的距離依次是:20 20 80 20(左,右,上,下),或許有人會疑問爲何距離右寫的是-20,why, 其實緣由很簡單,由於咱們參照的是self.view.trailing ,而視圖newView是加在self.view上,是在self.view.trailing 的左邊,因此應該是賦值,以此類推距離底部也是同樣
有人會問到底屬性都有哪些,下面會給你們列舉一下,這裏包括ios 8 最新添加的(一下加紅的是經常使用的一些方法)
NSLayoutAttributeLeft = 1, 對齊對象的左邊
NSLayoutAttributeRight, 對齊對象的右邊
NSLayoutAttributeTop, 距離頂部的距離
NSLayoutAttributeBottom, 距離底部的距離
NSLayoutAttributeLeading, 距離左邊的距離
NSLayoutAttributeTrailing, 距離右邊部的距離
NSLayoutAttributeWidth, 控件的寬度
NSLayoutAttributeHeight, 控件的高度
NSLayoutAttributeCenterX, x 軸中線點的距離
NSLayoutAttributeCenterY, y 軸中線點的距離
NSLayoutAttributeBaseline,
NSLayoutAttributeLastBaseline = NSLayoutAttributeBaseline,
// 在API 8.0 文檔中貌似沒有詳細的說明,但你們一看意思也明白了,就是距離各個邊界的設置
NSLayoutAttributeFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeLeftMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeRightMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeTopMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeBottomMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeLeadingMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeTrailingMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeCenterXWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeCenterYWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeNotAnAttribute = 0
注:NSLayoutAttributeLeft/NSLayoutAttributeRight 和 NSLayoutAttributeLeading/NSLayoutAttributeTrailing的區別是left/right永遠是指左右,而leading/trailing在某些從右至左習慣的地區會變成,leading是右邊,trailing是左邊。
以上就是經過簡單的代碼實現了自動佈局(單一控件),你們會發若是頁面上試圖多的話,用這種方式顯得特別麻煩,的確是的,下面我介紹一下另外一種方法
經過可視化語言的方式
先學點基礎知識,
1 調用的方法
- + (NSArray *)constraintsWithVisualFormat:(NSString *)format
- options:(NSLayoutFormatOptions)opts
- metrics:(NSDictionary *)metrics
- views:(NSDictionary *)views
介紹一下各個參數的意思
1 format 約束的規格要求 說白了就是條件。
2 opts 主要用來描述屬性和用來指導可視化語言中的佈局樣式。
3 metrics 一個字典實例主要用來顯示你在可視化話中用的字符串的參數設置,好比:nextwidth:@12, 必須是一個字典對象。
4 views 全部描述的空間字典集合,也必須以字典的形式展示出來。
下面經過一個實例的方式
- UIView *two = [UIView new];
- two.backgroundColor = [UIColor greenColor];
- [self.view addSubview:two];
- two.translatesAutoresizingMaskIntoConstraints = NO;
- NSDictionary *views = NSDictionaryOfVariableBindings(two, self.view);
-
- NSMutableArray *constraintArray = [NSMutableArray array];
- [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[two]-20-|"
- options:0
- metrics:nil
- views:views]];
- [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-80-[two]-20-|" options:0 metrics:nil views:views]];
- [self.view addConstraints:constraintArray];
簡單的幾句話就實現了和上面一樣的效果,從而能夠充分的看到可視化語言是多麼的方便
下面爲你們帶來介紹一些第三方實現該效果的代碼
首推:PureLayout 他是UIView+Layout 的後繼者,項目中建議使用PureLayout , 使用起來特方便,至於如何導入請你們參考文檔本身完成
下載地址:https://github.com/smileyborg/PureLayout
直接上代碼:
- UIView *newView = [UIView new];
- newView.backgroundColor = [UIColor blueColor];
- [self.view addSubview:newView];
-
- newView.translatesAutoresizingMaskIntoConstraints = NO;
- [newView autoPinEdgeToSuperviewEdge:(ALEdgeLeading) withInset:20];
- [newView autoPinEdgeToSuperviewEdge:(ALEdgeTrailing) withInset:20];
- [newView autoPinEdgeToSuperviewEdge:(ALEdgeTop) withInset:80];
- [newView autoPinEdgeToSuperviewEdge:(ALEdgeBottom) withInset:20];
你可能會很吃驚,哇?這麼簡單,的確是的就是這麼簡單,徹底符合咱們使用storyboard 的習慣,至於UIView+autoLayout 和 PureLayout的具體使用,我會在後面的文章中單獨拿出一章單獨介紹他的使用,如今主要是瞭解一下自動佈局的使用
先看一下效果圖,橫屏效果圖
![](http://static.javashuo.com/static/loading.gif)
豎屏效果圖
![](http://static.javashuo.com/static/loading.gif)
下面我們來點有難度的代碼,嘿嘿,終於能夠放手作一下了
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
-
-
-
- UIView *redView = [self alive];
- redView.backgroundColor = [UIColor redColor];
- [self.view addSubview:redView];
-
- UIView *greenView = [self alive];
- greenView.backgroundColor = [UIColor greenColor];
- [self.view addSubview:greenView];
-
- UIView *blueView = [self alive];
- blueView.backgroundColor = [UIColor blueColor];
- [self.view addSubview:blueView];
-
- UIView *yellowView = [self alive];
- yellowView.backgroundColor = [UIColor yellowColor];
- [self.view addSubview:yellowView];
-
- UIView *orangeView = [self alive];
- orangeView.backgroundColor = [UIColor orangeColor];
- [self.view addSubview:orangeView];
-
- [self.view addConstraints:[self portraitConstraints:redView :greenView :blueView yellowView:yellowView orangeView:orangeView]];
-
- }
- - (UIView *)alive
- {
- UIView *newView = [UIView new];
- newView.translatesAutoresizingMaskIntoConstraints = NO;
- return newView;
- }
- - (NSMutableArray *) portraitConstraints:(UIView *)redView :(UIView *)greenView :(UIView *)blueView yellowView:(UIView *)yellowView orangeView:(UIView *)orangeView
- {
- NSMutableArray *array = [NSMutableArray array];
-
- [array addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[redView(>=10,<=30)]-20-[greenView(>=40,<=120)]-20-[yellowView]-20-|" options:0 metrics:nil
- views:NSDictionaryOfVariableBindings(redView, greenView, yellowView)]];
-
- [array addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-70-[redView(>=100,<=160)]-20-[blueView(>=30,<=60)]-[orangeView]-20-|" options:0 metrics:nil
- views:NSDictionaryOfVariableBindings(redView, blueView, orangeView)]];
-
- [array addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-70-[greenView(>=100,<=160)]-20-[blueView(>=30,<=60)]-[orangeView]-20-|" options:0 metrics:nil
- views:NSDictionaryOfVariableBindings(greenView, blueView, orangeView)]];
-
- [array addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-70-[yellowView(>=100,<=160)]-20-[blueView(>=30,<=60)]-[orangeView]-20-|" options:0 metrics:nil
- views:NSDictionaryOfVariableBindings(yellowView, blueView, orangeView )]];
-
-
-
- [array addObjectsFromArray:[NSLayoutConstraint
- constraintsWithVisualFormat:@"H:|-20-[blueView]-120-|" options:0 metrics:nil
- views:NSDictionaryOfVariableBindings(blueView)]];
-
- [array addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[blueView]-20-[orangeView(>=blueView)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(blueView, orangeView)]];
-
-
- [array addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[orangeView]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(blueView, orangeView)]];
- return array;
- }