我正在拼命地將兩個視圖下面的對齊與自動佈局希望在未來的動畫和相對位置這使得它比剛剛設置框架和更新等更僵化。 。使用自動註銷將兩個uiviews彼此對齊使用自動註銷
因此,代碼:
// create two views
UIView *one = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
UIView *two = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 50, 50)];
// add some color
[one setBackgroundColor:[UIColor whiteColor]];
[two setBackgroundColor:[UIColor redColor]];
// add them to self (self = uiview)
[self addSubview:one];
[self addSubview:two];
// make dict
NSDictionary *views = NSDictionaryOfVariableBindings(one, two);
// add constraints to make the views as wide as the container (self)
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[one]|"
options:0
metrics:nil
views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[two]|"
options:0
metrics:nil
views:views]];
// add constraint to make the second (two) item 10px below the first (one)
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[one]-10-[two]"
options:0
metrics:nil
views:views]];
```
< - 結果
< - 目標
我在做什麼錯?
在此先感謝!
所以更容易使用Interface Builder做到這一點。 – PaulG