gpt4 book ai didi

ios - 为什么当我尝试创建按钮组时按钮折叠了?

转载 作者:行者123 更新时间:2023-11-29 02:22:02 24 4
gpt4 key购买 nike

我正在尝试水平创建一个 UIButton 组。

所以我创建了两个按钮并创建了一个新 View 来包含它们。然后我添加约束以使其水平均匀放置。

但是两个 UIButtons 刚刚折叠起来。按钮的初始位置是相同的,但我认为这并不重要。

- (UIButton*) getButtonWithTitle: (NSString*) title
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, 80, 130.0, 40.0);

button.layer.borderColor = [UIColor blackColor].CGColor;
button.layer.borderWidth = 0.5f;
button.layer.cornerRadius = 2.0f;

[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
return button;
}

- (void)viewDidLoad {
[super viewDidLoad];

UIButton *loginBtn = [self getButtonWithTitle:@"Login"];
UIButton *registerBtn = [self getButtonWithTitle:@"Register"];

UIView *buttonGroup = [[UIView alloc] init];
[buttonGroup setTranslatesAutoresizingMaskIntoConstraints:NO];
[buttonGroup addSubview: loginBtn];
[buttonGroup addSubview: registerBtn];
[self.view addSubview: buttonGroup];

NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(loginBtn, registerBtn, buttonGroup);
[buttonGroup addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[loginBtn]-20-[registerBtn]-|"
options:0 metrics:nil
views:viewsDictionary
]];
}

编辑

我按照deimus并删除了buttonGroup,但没有变化:

enter image description here

最佳答案

从此行中删除 buttonGroup 并重试。

NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(loginBtn, registerBtn, buttonGroup);

同时删除

button.frame = CGRectMake(0, 80, 130.0, 40.0);

当您选择使用 AutoLayouting 提供手动坐标不再有意义时

关于ios - 为什么当我尝试创建按钮组时按钮折叠了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28028176/

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