以编程方式添加自动布局视图不显示

时间:2014-02-25 19:56:33

标签: ios autolayout

我有一个带有表的视图控制器(通过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: enter image description here

知道按钮没有显示的原因吗?

1 个答案:

答案 0 :(得分:0)

啊,我想我明白了。你想要alignToBottomOfTable约束的负常量。正18的常数是将按钮推到桌子底部

NSLayoutConstraint *alignToBottomOfTable = [NSLayoutConstraint constraintWithItem:testButton
                                                                        attribute:NSLayoutAttributeTop
                                                                        relatedBy:NSLayoutRelationEqual
                                                                           toItem:availableStreamsTableView
                                                                        attribute:NSLayoutAttributeBottom
                                                                       multiplier:1 constant:-18];