我有一个带有表的视图控制器(通过Interface Builder定义)。我试图以编程方式从表格底部添加一个18px的按钮。
我在loadView中有以下内容:
- (void)loadView {
[super loadView];
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeSystem];
testButton.titleLabel.font = [UIFont fontWithName:@"System" size:13];
[testButton setTitle:@"Logout" forState:UIControlStateNormal];
[testButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:testButton];
self.testButton = testButton;
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:testButton attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.twitterAuthButton
attribute:NSLayoutAttributeWidth
multiplier:1 constant:0]];
NSLayoutConstraint *centerXConstraint = [NSLayoutConstraint constraintWithItem:testButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0 constant:0.0];
NSLayoutConstraint *alignToBottomOfTable = [NSLayoutConstraint constraintWithItem:testButton
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:availableStreamsTableView
attribute:NSLayoutAttributeBottom
multiplier:1 constant:18];
[self.view addConstraint:centerXConstraint];
[self.view addConstraint:alignToBottomOfTable];
}
但是按钮没有显示。不知道为什么。当我调试(在AppCode中)时,它似乎在那里,在右边的x和y:
知道按钮没有显示的原因吗?
答案 0 :(得分:0)
NSLayoutConstraint *alignToBottomOfTable = [NSLayoutConstraint constraintWithItem:testButton
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:availableStreamsTableView
attribute:NSLayoutAttributeBottom
multiplier:1 constant:-18];