NSLayoutConstraint-程式碼實現自動佈局的函式用法說明
1 2 3 4 5 6 7 |
[NSLayoutConstraint
constraintWithItem:(id)item
attribute:(NSLayoutAttribute)attribute
relatedBy:(NSLayoutRelation)relation
toItem:(id)otherItem
attribute:(NSLayoutAttribute)otherAttribute
multiplier:(CGFloat)multiplier
constant:(CGFloat)constant]
|
1 2 3 4 5 6 7 |
[NSLayoutConstraint
constraintWithItem:view1
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:view2
attribute:NSLayoutAttributeRight
multiplier:1
constant:10]
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
typedef NS_ENUM(NSInteger,
NSLayoutRelation) {
NSLayoutRelationLessThanOrEqual
= -1, //小於等於
NSLayoutRelationEqual
= 0, //等於
NSLayoutRelationGreaterThanOrEqual
= 1, //大於等於
};
typedef NS_ENUM(NSInteger,
NSLayoutAttribute) {
NSLayoutAttributeLeft
= 1, //左側
NSLayoutAttributeRight, //右側
NSLayoutAttributeTop, //上方
NSLayoutAttributeBottom, //下方
NSLayoutAttributeLeading, //首部
NSLayoutAttributeTrailing, //尾部
NSLayoutAttributeWidth, //寬度
NSLayoutAttributeHeight, //高度
NSLayoutAttributeCenterX, //X軸中心
NSLayoutAttributeCenterY, //Y軸中心
NSLayoutAttributeBaseline, //文字底標線
NSLayoutAttributeNotAnAttribute
= 0 //沒有屬性
};
|
螢幕相容的問題
為了讓我們的應用在不容尺寸的螢幕下都能 “正常”的表示,我們儘量不要把資料寫死。
大多數可視元素都是一個矩形區域,當然這個矩形區域有座標的,我們有了這個區域座標就能確定可視元素的現實位置了。
但是iphone5 和以前的螢幕不一樣了,在以前的裝置中,我們可以新增一個 [email protected] 來適應retina螢幕,但是iphoen5咋辦呢?
ios 引入了 Auto Layout 的東東,這個要和UIViewAutoresizing 區分下。
看下面程式碼
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *aView = [[UIView alloc] init];
aView.backgroundColor = [UIColor redColor];
//為了不和autosizing衝突,我們設定No
[aView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:aView];
UIView *bView = [[UIView alloc] init];
bView.backgroundColor = [UIColor blueColor];
[bView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:bView];
NSDictionary *views = NSDictionaryOfVariableBindings(aView, bView);
//NSDictionaryOfVariableBindings 巨集 其實 NSDictionaryOfVariableBindings(v1, v2, v3) 等效於 [NSDictionary dictionaryWithObjectsAndKeys:v1, @"v1", v2, @"v2", v3, @"v3", nil];
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=50)-[aView(100)]"
options:0
metrics:nil
views:views]];
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=100)-[aView(50)]"
options:0
metrics:nil
views:views]];
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:[bView(==aView)]"
options:0
metrics:nil
views:views]];
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:[bView(==aView)]"
options:0
metrics:nil
views:views]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:bView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:aView
attribute:NSLayoutAttributeRight
multiplier:1
constant:10]];
//新增一個限制 等效於 bView.frame.origin.x = (aView.frame.origin.x +aView.frame.size.width) * 1 + 10,好像是這樣的!個人覺得!
它是一種依賴關係,bView依賴aView,這樣就算aView變了,bView也會跟著變換。
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:bView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:aView
attribute:NSLayoutAttributeTop
multiplier:1
constant:0]];
[aView release];
[bView release];
}
constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:
Create a constraint of the form "view1.attr1 <relation> view2.attr2 * multiplier + constant".
屬性
最後的結果就是 “view1.attr1 < >= 或者 == 或者 <= > view2.attr2 * multiplier + constant”