gpt4 book ai didi

ios - 如何在 Objective c 中以编程方式在 UIView 中以相同的宽度放置 2 个 UIButton?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:58:22 25 4
gpt4 key购买 nike

我使用了这段代码(在 UIView 类中)。

- (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

我正在使用第三方库。不知道直接逻辑不起作用。如何设置两个按钮的框架?期待自动布局代码。提前致谢。

最佳答案

您可以将按钮的框架设置为 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];

}

关于ios - 如何在 Objective c 中以编程方式在 UIView 中以相同的宽度放置 2 个 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34871812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com