自動布局方法1
/**
創(chuàng)建約束
@param view1 被約束視圖view1
@param attr1 被約束視圖的布局屬性
@param relation 關系屬性
@param view2 參照視圖view2
@param attr2 參照視圖view2的布局屬性
@param multiplier 倍數
@param c 常數
@return 約束
*/
+(instancetype)constraintWithItem:(id)view1
attribute:(NSLayoutAttribute)attr1
relatedBy:(NSLayoutRelation)relation
toItem:(nullable id)view2
attribute:(NSLayoutAttribute)attr2
multiplier:(CGFloat)multiplier
constant:(CGFloat)c;
依據的公式是:view1.attr1 = view2.attr2 * multiplier + constant
NSLayoutAttribute(布局屬性)
NSLayoutAttribute的類型 | 含義 |
---|---|
NSLayoutAttributeLeft | 視圖的左邊 |
NSLayoutAttributeRight | 視圖的右邊 |
NSLayoutAttributeTop | 視圖的上邊 |
NSLayoutAttributeBottom | 視圖的下邊 |
NSLayoutAttributeLeading | 視圖的前邊 |
NSLayoutAttributeTrailing | 視圖的后邊 |
NSLayoutAttributeWidth | 視圖的寬度 |
NSLayoutAttributeHeight | 視圖的高度 |
NSLayoutAttributeCenterX | 視圖的中點的X值 |
NSLayoutAttributeCenterY | 視圖中點的Y值 |
NSLayoutAttributeBaseline | 視圖的基準線 |
NSLayoutAttributeNotAnAttribute | 無屬性 |
NSLayoutRelation(關系屬性)
NSLayoutRelation的類型 | 含義 |
---|---|
NSLayoutRelationLessThanOrEqual | 視圖關系小于或等于 |
NSLayoutRelationEqual | 視圖關系等于 |
NSLayoutRelationGreaterThanOrEqual | 視圖關系大于或等于 |
代碼提示
<#被約束視圖#>.translatesAutoresizingMaskIntoConstraints = NO;
[<#view的父視圖#> addConstraint:[NSLayoutConstraint constraintWithItem:<#被約束視圖#> attribute:(NSLayoutAttribute<#被約束視圖的布局屬性#>) relatedBy:(NSLayoutRelation<#布局屬性的布局關系#>) toItem:<#參照視圖#> attribute:NSLayoutAttribute<#參照視圖的布局屬性#> multiplier:<#倍數#> constant:<#常數#>]];
代碼示例
- (void)codeLayoutConstraint {
//創(chuàng)建背景圖
UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:backgroundView];
backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
//和父視圖等寬
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:backgroundView attribute:(NSLayoutAttributeWidth) relatedBy:(NSLayoutRelationEqual) toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
//二分之一高
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:backgroundView attribute:(NSLayoutAttributeHeight) relatedBy:(NSLayoutRelationEqual) toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0]];
//垂直對齊
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:backgroundView attribute:(NSLayoutAttributeCenterX) relatedBy:(NSLayoutRelationEqual) toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
//水平對齊
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:backgroundView attribute:(NSLayoutAttributeCenterY) relatedBy:(NSLayoutRelationEqual) toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
//創(chuàng)建橙色視圖
UIView *orangeView = [[UIView alloc] init];
orangeView.backgroundColor = [UIColor orangeColor];
[backgroundView addSubview:orangeView];
orangeView.translatesAutoresizingMaskIntoConstraints = NO;
//上邊距離父視圖上邊20
[backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:orangeView attribute:(NSLayoutAttributeTop) relatedBy:(NSLayoutRelationEqual) toItem:backgroundView attribute:NSLayoutAttributeTop multiplier:1 constant:20]];
//下邊距離父視圖下邊減20
[backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:orangeView attribute:(NSLayoutAttributeBottom) relatedBy:(NSLayoutRelationEqual) toItem:backgroundView attribute:NSLayoutAttributeBottom multiplier:1 constant:-20]];
if (/* DISABLES CODE */ (YES)) {//if和else效果一樣
//前邊距離父視圖前邊20
[backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:orangeView attribute:(NSLayoutAttributeLeading) relatedBy:(NSLayoutRelationEqual) toItem:backgroundView attribute:NSLayoutAttributeLeading multiplier:1 constant:20]];
//后邊距離父視圖后邊減100
[backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:orangeView attribute:(NSLayoutAttributeTrailing) relatedBy:(NSLayoutRelationEqual) toItem:backgroundView attribute:NSLayoutAttributeTrailing multiplier:1 constant:-100]];
} else {
//左邊距離父視圖前邊20
[backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:orangeView attribute:(NSLayoutAttributeLeft) relatedBy:(NSLayoutRelationEqual) toItem:backgroundView attribute:NSLayoutAttributeLeft multiplier:1 constant:20]];
//右邊距離父視圖后邊減100
[backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:orangeView attribute:(NSLayoutAttributeRight) relatedBy:(NSLayoutRelationEqual) toItem:backgroundView attribute:NSLayoutAttributeRight multiplier:1 constant:-100]];
}
//創(chuàng)建紅色視圖
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[backgroundView addSubview:redView];
redView.translatesAutoresizingMaskIntoConstraints = NO;
//上邊距離父視圖上邊20
[backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:(NSLayoutAttributeTop) relatedBy:(NSLayoutRelationEqual) toItem:backgroundView attribute:NSLayoutAttributeTop multiplier:1 constant:20]];
//高度44
[backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:44]];
//自身寬高比1:1
[backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:(NSLayoutAttributeHeight) relatedBy:(NSLayoutRelationEqual) toItem:redView attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
if (/* DISABLES CODE */ (NO)) {//if和else效果一樣
//左邊距離橙色視圖右邊20
[backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:(NSLayoutAttributeLeft) relatedBy:(NSLayoutRelationEqual) toItem:orangeView attribute:NSLayoutAttributeRight multiplier:1 constant:20]];
} else {
//前邊距離橙色視圖后邊20
[backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:(NSLayoutAttributeLeading) relatedBy:(NSLayoutRelationEqual) toItem:orangeView attribute:NSLayoutAttributeTrailing multiplier:1 constant:20]];
}
}
自動布局方法2(了解即可)
@param format 使用VFL格式化的字符串力细,可以參見官方的幫助文檔贪绘;
@param opts 指定VFL中所有對象的布局屬性和方向净响。舉例:有2個視圖使用VFL進行布局,可以使用NSLayoutFormatAlignAllLeft碎连,就讓兩個視圖左對齊;
@param metrics 度量或者指標的字典累驮,字典里面有相關的鍵值對來控制相關的度量指標卫枝,通過key獲燃灞;
@param views 指定約束的視圖:一個或多個剃盾。
+ (NSArray<__kindof NSLayoutConstraint *> *)constraintsWithVisualFormat:(NSString *)format
options:(NSLayoutFormatOptions)opts
metrics:(nullable NSDictionary<NSString *,id> *)metrics
views:(NSDictionary<NSString *, id> *)views;
VFL字符串說明:
VFL字符串說明.png
代碼示例
- (void)codeLayoutConstraint2 {
UIView *yellowView = [[UIView alloc] init];
yellowView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:yellowView];
yellowView.translatesAutoresizingMaskIntoConstraints = NO;
UIView *orangeView = [[UIView alloc] init];
orangeView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:orangeView];
orangeView.translatesAutoresizingMaskIntoConstraints = NO;
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
redView.translatesAutoresizingMaskIntoConstraints = NO;
//創(chuàng)建VFL約束字符串
//水平順序:父視圖腺占,space個距離,黃色寬100痒谴,5個距離衰伯,橙色視圖,space1個距離积蔚,父視圖
NSString *hVFL = @"H:|-space-[yellowView(100)]-5-[orangeView]-space1-|";
//水平順序:父視圖意鲸,space個距離,紅色視圖寬和橙色視圖相等
NSString *hVFL1 = @"H:|-space-[redView(==orangeView)]";
//垂直順序:父視圖尽爆,100個距離怎顾,橙色視圖高100
NSString *vVFL = @"V:|-100-[orangeView(==100)]";
//垂直順序:父視圖,100個距離漱贱,黃色視圖高100槐雾,5個距離,紅色視圖和橙色視圖高相等
NSString *vVFL1 = @"V:|-100-[yellowView(==100)]-5-[redView(==orangeView)]";
//度量值
NSDictionary *metircs = @{@"space":@15,@"space1":@30};
//views
NSDictionary *views = NSDictionaryOfVariableBindings(yellowView,orangeView,redView);
//創(chuàng)建約束
NSArray *hconstraint = [NSLayoutConstraint constraintsWithVisualFormat:hVFL options:NSLayoutFormatDirectionLeadingToTrailing metrics:metircs views:views];
NSArray *hconstraint1 = [NSLayoutConstraint constraintsWithVisualFormat:hVFL1 options:NSLayoutFormatAlignAllLeft | NSLayoutFormatAlignAllTop metrics:metircs views:views];
NSArray *vconstraint = [NSLayoutConstraint constraintsWithVisualFormat:vVFL options:NSLayoutFormatDirectionLeadingToTrailing metrics:metircs views:views];
NSArray *vconstraint1 = [NSLayoutConstraint constraintsWithVisualFormat:vVFL1 options:NSLayoutFormatDirectionLeadingToTrailing metrics:metircs views:views];
//添加約束
[self.view addConstraints:hconstraint];
[self.view addConstraints:hconstraint1];
[self.view addConstraints:vconstraint];
[self.view addConstraints:vconstraint1];
}