gpt4 book ai didi

ios - Objective-C : Adding UI button with constraints programatically

转载 作者:行者123 更新时间:2023-11-28 18:57:42 27 4
gpt4 key购买 nike

我想创建固定尺寸的按钮,无论屏幕尺寸和方向如何,它都设置为与右上角的固定距离,但无法实现。

我已经尝试了以下代码,它仅在纵向模式下正确显示,因为框架位置是固定的。

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.closeBtn addTarget:self action:@selector(closeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];

//NSLayoutConstraint* doneconstraint = [NSLayoutConstraint constraintWithItem:self.closeBtn attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:5.0];
//[self addConstraint:doneconstraint];
[self.view addSubview:self.closeBtn];

我也试过添加约束[取消注释 2 行],但它给出了以下错误

Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

我对错误的猜测是框架和约束指定了按钮的不同位置,但我是新手,所以我不确定发生了什么以及如何修复。

一个解决方案是改变旋转框架,但我认为使用约束会是更简洁的方法。

最佳答案

试试这个代码,只需将 60 替换为你想要的

 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]];

截图是

enter image description here

关于ios - Objective-C : Adding UI button with constraints programatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30482759/

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