【问题标题】:Objective C: Adding UI button with constraints programatically目标 C:以编程方式添加带有约束的 UI 按钮
【发布时间】:2015-05-27 12:40:35
【问题描述】:

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

我尝试了以下代码,该代码仅在框架位置固定后才能在纵向模式下正确显示。

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 行],但它给出了以下错误

无法同时满足约束。 以下列表中的至少一个约束可能是您不想要的。试试这个:(1)查看每个约束并尝试找出您不期望的; (2) 找到添加了一个或多个不需要的约束的代码并修复它。 (注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档)

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

一种解决方案是在旋转时更改框架,但我认为使用约束会更简洁。

【问题讨论】:

    标签: ios objective-c uibutton autolayout


    【解决方案1】:

    试试这个代码,把 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]];
    

    截图是

    【讨论】:

    • 我没有取消注释该代码,而是放置了您提供的代码,但出现了与我的代码相同的错误。
    • 是的,有效。你做了什么?看起来都一样!
    • 我忘记粘贴这一行self.closeBtn.translatesAutoresizingMaskIntoConstraints = NO; 。如果有效,请接受我的回答。谢谢
    • 非常感谢。所以现在我需要阅读 translatesAutoresizingMaskIntoConstraints
    • 按钮的宽度减小了。我尝试将 CGRectMake 中的尺寸从 50 更改为 70,但没有任何反应。
    猜你喜欢
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 2015-11-21
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多