1. 程式人生 > >iOS代碼添加視圖約束

iOS代碼添加視圖約束

tro targe get alpha 版本號 不能 welcome 技術 eight

項目要做這樣一個效果的啟動頁。

技術分享

考慮到版本號是會不斷變更的,因此采用動畫效果啟動頁,讓版本號動態加載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代碼添加視圖約束