標籤:iap .text block animation etop self 啟動 size dpi
項目要做這樣一個效果的啟動頁。
考慮到版本號碼是會不斷變更的,因此採用動畫效果啟動頁,讓版本號碼動態載入iOS啟動頁動畫效果 - 簡書
考慮到螢幕適配問題,因此採用代碼對視圖添加約束。在添加約束的過程中遇到了一些問題,在此做一下記錄和總結.
代碼實現autolayout的注意點:
1.要先禁止autoresizing功能,設定view 的translatesAutoresizingMaskIntoConstraints 屬性為 NO;
2.添加約束之前,一定要保證相關控制項都已經在各自的父控制項上。(就是要先addsubview,然後再添加約束addConstraint:)
3.不用再給view設定frame
先上代碼
welcome.h(建立一個繼承自UIView的welcome類)
#[email protected] 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設定frame
_imageView = [[UIImageView alloc]init];
_imageView.image = bgImage;
[self addSubview:_imageView];//先,保證控制項已經在父控制項上
[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];//注意:有小數點
_lab.textAlignment = NSTextAlignmentCenter;
_lab.text = @"清新空氣,淨化生活";
[self addSubview:_lab];
[self addLabConstraint];
......
}
給圖片添加約束
- (void)addPicConstraint{
//禁用antoresizing
_imageView.translatesAutoresizingMaskIntoConstraints = NO;
//建立約束
//添加高約束
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方向的約束添加到子控制項身上,這是不對的,必然會報錯。
約束添加規則總結:
1.約束不依賴於其他控制項(添加的約束和其他控制項沒有關係),會添加到自己身上
2.如果是父子關係,設定子控制項的約束,約束添加到父控制項上
3.如果是兄弟關係,設定兩兄弟的約束,約束會添加到第一個共同的父控制項上
ps:另外還有一個要注意的地方,用代碼給UILable的文字設定顏色,一開始的時候出現了[UIColor colorWithRed: green: blue: alpha:] 失效問題。
網上搜尋了一下,發現了問題的所在:RGB的顏色值範圍都是在0.0~1.0之間的,並不是我們誤認為的0~255。
正確的用法:_lab.textColor = [UIColor colorWithRed:46.0/255 green:168.0/255 blue:23.0/255 alpha:1.0];而且要注意上面四個參數都是float類型的(所以不能寫成46/255)
iOS代碼添加視圖約束