2016-01-19 24 views
2

我用這個代碼(在UIView類中)。如何在Objective-C中以相同寬度以編程方式在UIView上放置2個UIButton?

- (void)setup { 

    self.backgroundColor = [UIColor colorWithWhite:0.12f alpha:1.0f]; 

    _doneTextButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    _doneTextButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 
    [_doneTextButton setTitle:@"Save" forState:UIControlStateNormal]; 
    [_doneTextButton setTitleColor:CODE_A_COLOR forState:UIControlStateNormal]; 

    [_doneTextButton setBackgroundColor:CODE_6_COLOR] ; 
    [_doneTextButton.titleLabel setFont:[FontManager getOpenSansBoldFontWithSize:16]]; 
    [_doneTextButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 

    [self addSubview:_doneTextButton]; 
} 

我得到這個輸出(按鈕在角落)。

enter image description here

我想這個 「取消」 和 「保存」 按鈕恰好UIView的

enter image description here

我使用第三方的lib同樣適合。並沒有想法直邏輯不工作。我如何設置兩個按鈕的框架?期待autoLayout代碼。提前致謝。

+1

您使用的是自動佈局 –

回答

2

可以設置按鈕self.view.frame.size.width/2的框架,然後你可以通過第一按鈕的終點

-(void)setup 
{ 
btnSave = [UIButton buttonWithType:UIButtonTypeCustom]; 
btnCancel = [UIButton buttonWithType:UIButtonTypeCustom]; 

btnSave.frame = CGRectMake(0, self.view.frame.size.height-30, self.view.frame.size.width/2, 30); 

btnCancel.frame = CGRectMake(btnSave.frame.origin.x+btnSave.frame.size.width+5, self.view.frame.size.height-30, btnSave.frame.size.width, 30); 

btnSave.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; 
[btnSave setTitle:@"Save" forState:UIControlStateNormal]; 
[btnSave setBackgroundColor:[UIColor blackColor]] ; 
[self.view addSubview:btnSave]; 

btnCancel.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; 
[btnCancel setTitle:@"Cancel" forState:UIControlStateNormal]; 
[btnCancel setBackgroundColor:[UIColor blackColor]] ; 
[self.view addSubview:btnCancel]; 

} 
0

請使用此解決方案,如果你不打算設定的第二鈕框架自動佈局。

看來你要設置寬度與您的觀點的frame like self.view.frame.size.width。在某些情況下,它會返回與您在xib或storyboard中定義的尺寸相同的尺寸。所以這就是爲什麼它的發生,更多信息請看看

UIViewController viewDidLoad incorrect width/height

請使用此解決方案來擺脫這些問題。

float width = [UIScreen mainScreen].bounds.size.width; 

self.cancelButton.frame = CGRectMake(0, self.view.frame.size.height+self.view.frame.origin.y-40, width/2, 40); 

self.doneButton.frame = CGRectMake(self.cancelButton.frame.size.width+self.cancelButton.frame.origin.x+1, self.view.frame.size.height+self.view.frame.origin.y-40, width/2, 40); 
1

我想你在故事板中使用自動佈局,所以你必須使用佈局約束來實現這種行爲。

創建框架等於CGRectZero的按鈕,然後在updateViewConstraints方法中,您可以創建設置按鈕位置所需的約束。這裏是一個例子:

-(void)updateViewConstraints { 
    [super updateViewConstraints]; 
    if(!constraintsAdded) { 
     [button1 setTranslatesAutoresizingMaskIntoConstraints:NO]; 
     [button2 setTranslatesAutoresizingMaskIntoConstraints:NO]; 

     [self.view addConstraint:[NSLayoutConstraint constraintWithItem:button1 
                  attribute:NSLayoutAttributeWidth 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:button2 
                  attribute:NSLayoutAttributeWidth 
                 multiplier:1.0 
                  constant:0.0]]; 
     constraintsAdded = true; 
    } 
} 
0

使用UIStackView.and在堆棧視圖中添加兩個按鈕。

相關問題