2014-02-25 84 views
0

我有一個視圖控制器與表(通過界面生成器定義)。我試圖以編程方式從表底部添加一個按鈕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

任何想法爲什麼按鈕不顯示?

回答

0

啊,我想我明白了。你想爲你的alignToBottomOfTable約束設置一個負常數。正數18常數將您的按鈕放在以下的桌子底部。

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

謝謝,這確實有所作爲。現在出現該按鈕。但這不是我想要的。按鈕與表格重疊。很明顯,我不太瞭解自動佈局。我希望按鈕始終顯示在桌子後面。我正在閱讀https://developer.apple.com/library/ios/documentation/userexperience/conceptual/AutolayoutPG/AutoLayoutbyExample/AutoLayoutbyExample.html#//apple_ref/doc/uid/TP40010853-CH5-SW2以查看我是否可以使用滾動視圖並將按鈕固定到底部。 – septerr

相關問題