简体   繁体   English

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

[英]Programmatically added auto layout view not displaying

I have a view controller with a table (defined through Interface Builder). 我有一个带有表的视图控制器(通过Interface Builder定义)。 I am trying to programmatically add a button 18px from the bottom of the table. 我正在尝试以编程方式从表格底部添加一个18px的按钮。

I have following in loadView: 我在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];

} }

But the button is not displaying. 但是该按钮未显示。 Not sure why. 不知道为什么。 When I debug (in AppCode), it seems to be there and at the right x and y: 当我调试(在AppCode中)时,它似乎在那里并且在正确的x和y处: 在此处输入图片说明

Any idea why the button is not showing? 知道为什么按钮不显示吗?

Ah, I think I see it. 嗯,我想我明白了。 You want a negative constant for your alignToBottomOfTable constraint. 您需要一个alignToBottomOfTable约束的负常量。 The positive 18 constant is pushing your button below the bottom of your table. 正的18常数将您的按钮按表格底部下方。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM