gpt4 book ai didi

ios - 需要在底部中心为我的 UIButton 设置自动布局约束使用代码

转载 作者:行者123 更新时间:2023-11-28 18:56:00 24 4
gpt4 key购买 nike

这是我更新的问题!

我搜索了许多教程和网站来设置自动布局约束以将我的 UIButton 设置在我的 View Controller 的底部中心。我使用代码创建 uiButton 并且我已经设置了位置,但通常我可以看到我的 uiButton 定位在不同模拟器中的不同位置(4S、5、6、INFACT 在我自己的设备上)。我需要将我的 uibutton 设置在底部中心 Like this image

我是 ios 的新手,所以无法为我的 UIButton 设置约束。这是我的 UIButton 代码:

self->closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self->closeBtn.frame = CGRectMake(260, 30, 50, 28);
self->closeBtn.layer.cornerRadius = 4;
self->closeBtn.layer.borderWidth = 1;
self->closeBtn.layer.borderColor = [UIColor colorWithRed:179.0/255.0 green:179.0/255.0 blue:179.0/255.0 alpha:1.0].CGColor;
[self->closeBtn setTitleColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal];
self->closeBtn.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
[self->closeBtn setTitle:@"Done" forState:UIControlStateNormal];
[self->closeBtn.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0]];
[self.view addSubview:self->closeBtn];
[self->closeBtn addTarget:self action:@selector(closeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
self->closeBtn.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint * c_1 =[NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self->closeBtn
attribute:NSLayoutAttributeRight
multiplier:1.0 constant:60];
NSLayoutConstraint * c_2 =[NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self->closeBtn
attribute:NSLayoutAttributeTop
multiplier:1.0 constant:-1*60];



NSLayoutConstraint * equal_w = [NSLayoutConstraint constraintWithItem:self->closeBtn
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1.0
constant:70];
NSLayoutConstraint * equal_h = [NSLayoutConstraint constraintWithItem:self->closeBtn
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1.0
constant:28];
[self.view addConstraints:@[c_1,c_2]];
[self->closeBtn addConstraints:@[equal_w,equal_h]];

上面的代码设置在右上角。所以我已将其更改为底部,中心,但我看不到我的按钮。我需要我的按钮 Like this image button position无法设置约束以将我的 uibutton 位置放在同一个地方。任何人都可以帮助我解决我的问题

最佳答案

我已经检查了您的代码。将下面的代码替换为您的代码。您可以根据需要将按钮放在所需的位置

UIView *superview = self.view;
// Do any additional setup after loading the view, typically from a nib.
self->closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self->closeBtn.frame = CGRectMake(260, 100, 50, 28);
self->closeBtn.layer.cornerRadius = 4;
self->closeBtn.layer.borderWidth = 1;
self->closeBtn.layer.borderColor = [UIColor colorWithRed:179.0/255.0 green:179.0/255.0 blue:179.0/255.0 alpha:1.0].CGColor;
[self->closeBtn setTitleColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal];
self->closeBtn.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
[self->closeBtn setTitle:@"Done" forState:UIControlStateNormal];
[self->closeBtn.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0]];
[self.view addSubview:self->closeBtn];
[self->closeBtn addTarget:self action:@selector(closeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
self->closeBtn.translatesAutoresizingMaskIntoConstraints = NO;

NSLayoutConstraint * c_1 =[NSLayoutConstraint
constraintWithItem:self->closeBtn attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
NSLayoutAttributeCenterX multiplier:1.0 constant:-7.5f];


NSLayoutConstraint * c_2 =[NSLayoutConstraint
constraintWithItem:self->closeBtn attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual toItem:superview attribute:
NSLayoutAttributeCenterY multiplier:1.85f constant:0.0f];


NSLayoutConstraint * equal_w = [NSLayoutConstraint constraintWithItem:self->closeBtn
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1.0
constant:50];
NSLayoutConstraint * equal_h = [NSLayoutConstraint constraintWithItem:self->closeBtn
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1.0
constant:28];
[self.view addConstraints:@[c_1,c_2]];
[self->closeBtn addConstraints:@[equal_w,equal_h]];

希望这对您有所帮助。请学习一些有关自动布局的教程。确保这些概念对制作应用程序非常有帮助...

关于ios - 需要在底部中心为我的 UIButton 设置自动布局约束使用代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32808065/

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