1
我有一個簡單視圖(控制器)具有兩個子視圖:加入NSLayoutConstraint使得視圖消失
- (void)viewDidLoad {
[super viewDidLoad];
UIView *viewA = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
viewA.translatesAutoresizingMaskIntoConstraints = NO;
viewA.backgroundColor = [UIColor redColor];
[self.view addSubview:viewA];
UIView *viewB = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
viewB.translatesAutoresizingMaskIntoConstraints = NO;
viewB.backgroundColor = [UIColor grayColor];
[self.view addSubview:viewB];
}
當然它們在這一點上(ⅰ只能看到viewB)重疊。所以我增加了一個約束,使viewB下面viewA,像這樣:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:viewB
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:viewA
attribute:NSLayoutAttributeBottom
multiplier:1.f
constant:0]];
這使得兩個視圖消失。我究竟做錯了什麼?
謝謝!
非常感謝! – 0xSina 2014-10-08 05:53:03