項目要做這樣一個效果的啟動頁。
考慮到版本號是會不斷變更的角寸,因此采用動畫效果啟動頁菩混,讓版本號動態(tài)加載iOS啟動頁動畫效果 - 簡書
考慮到屏幕適配問題,因此采用代碼對視圖添加約束扁藕。在添加約束的過程中遇到了一些問題沮峡,在此做一下記錄和總結(jié).
代碼實現(xiàn)autolayout的注意點:
1.要先禁止autoresizing功能,設(shè)置view 的translatesAutoresizingMaskIntoConstraints 屬性為 NO;
2.添加約束之前亿柑,一定要保證相關(guān)控件都已經(jīng)在各自的父控件上邢疙。(就是要先addsubview,然后再添加約束addConstraint:)
3.不用再給view設(shè)置frame
先上代碼
welcome.h(創(chuàng)建一個繼承自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ù)點
_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方向約束的代碼,一開始的時候我是這樣寫的式矫,以y方向為例
NSLayoutConstraint *picTop = [NSLayoutConstraint constraintWithItem:_bgImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_bgImageView.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
[_bgImageView addConstraint:picTop];
代碼運行的時候崩潰
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時,我在一個父view上添加了一個橙色的子view。并給它添加了寬高的約束(為固定值)以及相對父view上邊距故慈、左邊距的約束板熊。從截圖中看到,寬高的約束是添加在子控件自己身上的察绷,因為它不依賴于別的控件干签。而xy方向的約束,則是添加在父控件上拆撼。所以上面代碼容劳,把相對于父控件的y方向的約束添加到子控件身上,這是不對的闸度,必然會報錯竭贩。
約束添加規(guī)則總結(jié):
1.約束不依賴于其他控件(添加的約束和其他控件沒有關(guān)系),會添加到自己身上
2.如果是父子關(guān)系莺禁,設(shè)置子控件的約束留量,約束添加到父控件上
3.如果是兄弟關(guān)系,設(shè)置兩兄弟的約束哟冬,約束會添加到第一個共同的父控件上
ps:另外還有一個要注意的地方楼熄,用代碼給UILable的文字設(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];而且要注意上面四個參數(shù)都是float類型的(所以不能寫成46/255)