項(xiàng)目要做這樣一個(gè)效果的啟動(dòng)頁(yè)。
考慮到版本號(hào)是會(huì)不斷變更的,因此采用動(dòng)畫效果啟動(dòng)頁(yè),讓版本號(hào)動(dòng)態(tài)加載iOS啟動(dòng)頁(yè)動(dòng)畫效果 - 簡(jiǎn)書
考慮到屏幕適配問題,因此采用代碼對(duì)視圖添加約束。在添加約束的過程中遇到了一些問題,在此做一下記錄和總結(jié).
代碼實(shí)現(xiàn)autolayout的注意點(diǎn):
1.要先禁止autoresizing功能,設(shè)置view 的translatesAutoresizingMaskIntoConstraints 屬性為 NO;
2.添加約束之前,一定要保證相關(guān)控件都已經(jīng)在各自的父控件上。(就是要先addsubview,然后再添加約束addConstraint:)
3.不用再給view設(shè)置frame
先上代碼
welcome.h(創(chuàng)建一個(gè)繼承自UIView的welcome類)
#import@interface welcome :UIView
+ (instancetype)startView;
- (instancetype) initWithBgImage:(UIImage *)bgImage;
- (void) startAnimation;
@end
welcome.m
- (instancetype) initWithBgImage:(UIImage *)bgImage{
if (self= [super initWithFrame:[UIApplication sharedApplication].keyWindow.bounds]) {
self.backgroundColor = [UIColor whiteColor];
//? ? ? ? _imageView = [[UIImageView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];//不用再給view設(shè)置frame
_imageView = [[UIImageView alloc]init];
_imageView.image = bgImage;
[self addSubview:_imageView];//先,保證控件已經(jīng)在父控件上
[self addPicConstraint];//后
_lab = [[UILabel alloc]init];
_lab.font = [UIFont systemFontOfSize:20];
_lab.textColor = [UIColor colorWithRed:46.0/255 green:168.0/255 blue:23.0/255 alpha:1];//注意:有小數(shù)點(diǎn)
_lab.textAlignment = NSTextAlignmentCenter;
_lab.text = @"清新空氣,凈化生活";
[self addSubview:_lab];
[self addLabConstraint];
......
}
給圖片添加約束
- (void)addPicConstraint{
//禁用antoresizing
_imageView.translatesAutoresizingMaskIntoConstraints = NO;
//創(chuàng)建約束
//添加高約束
NSLayoutConstraint *picHeight = [NSLayoutConstraint constraintWithItem:_imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:70];
[_imageView addConstraint:picHeight];
//添加寬約束
NSLayoutConstraint *picWeight = [NSLayoutConstraint constraintWithItem:_imageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:70];
[_imageView addConstraint:picWeight];
//添加y方向約束
NSLayoutConstraint *picTop = [NSLayoutConstraint constraintWithItem:_imageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_imageView.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:60];[self addConstraint:picTop];
//添加x方向約束
NSLayoutConstraint *picVer = [NSLayoutConstraint constraintWithItem:_imageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:_imageView.superview attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
[self addConstraint:picVer];
}
留意上面添加xy方向約束的代碼,一開始的時(shí)候我是這樣寫的,以y方向?yàn)槔?/p>
NSLayoutConstraint *picTop = [NSLayoutConstraint constraintWithItem:_bgImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_bgImageView.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
[_bgImageView addConstraint:picTop];
代碼運(yùn)行的時(shí)候崩潰
The view hierarchy is not prepared for the constraint:When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.
2016-03-16 14:03:57.250 AirClean[22640:516239] View hierarchy unprepared for constraint.
想了很久也不知道是什么原因。最后再stackoverflow上找到了解決方法ios - Centering subview's X in autolayout throws "not prepared for the constraint" - Stack Overflow
崩潰的原因就是約束沒添加到正確的地方。
什么意思呢?看下面的例子
使用storyboard時(shí),我在一個(gè)父view上添加了一個(gè)橙色的子view。并給它添加了寬高的約束(為固定值)以及相對(duì)父view上邊距、左邊距的約束。從截圖中看到,寬高的約束是添加在子控件自己身上的,因?yàn)樗灰蕾囉趧e的控件。而xy方向的約束,則是添加在父控件上。所以上面代碼,把相對(duì)于父控件的y方向的約束添加到子控件身上,這是不對(duì)的,必然會(huì)報(bào)錯(cuò)。
約束添加規(guī)則總結(jié):
1.約束不依賴于其他控件(添加的約束和其他控件沒有關(guān)系),會(huì)添加到自己身上
2.如果是父子關(guān)系,設(shè)置子控件的約束,約束添加到父控件上
3.如果是兄弟關(guān)系,設(shè)置兩兄弟的約束,約束會(huì)添加到第一個(gè)共同的父控件上
ps:另外還有一個(gè)要注意的地方,用代碼給UILable的文字設(shè)置顏色,一開始的時(shí)候出現(xiàn)了[UIColor colorWithRed: green: blue: alpha:] 失效問題。
網(wǎng)上搜索了一下,發(fā)現(xiàn)了問題的所在:RGB的顏色值范圍都是在0.0~1.0之間的,并不是我們誤認(rèn)為的0~255。
正確的用法:_lab.textColor = [UIColor colorWithRed:46.0/255 green:168.0/255 blue:23.0/255 alpha:1.0];而且要注意上面四個(gè)參數(shù)都是float類型的(所以不能寫成46/255)