iOS NSLayoutConstraint 固定宽度使用constraintWithItem

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27681963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:09:16  来源:igfitidea点击:

iOS NSLayoutConstraint Fixed Width using constraintWithItem

iosobjective-cautolayout

提问by Connor

I'd like to set a constraint to give a UIButton a fixed (constant) width programmatically. I know I can do this with constraintsWithVisualFormat, but I've been using constraintWithItem for all of my constraints in code. I was wondering for the sake of curiosity/consistency if there was any way to do this with constraintWithItem.

我想设置一个约束,以编程方式为 UIButton 提供固定(恒定)宽度。我知道我可以使用constraintWithVisualFormat 来做到这一点,但我一直在使用constraintWithItem 来处理代码中的所有约束。为了好奇心/一致性,我想知道是否有任何方法可以使用constraintWithItem来做到这一点。

回答by Connor

Found my solution. Just set the other object to nil, and the other attribute to NSLayoutAttributeNotAnAttribute (this was what I failed to think of) and use the constant parameter for the fixed width:

找到了我的解决方案。只需将另一个对象设置为 nil,将另一个属性设置为 NSLayoutAttributeNotAnAttribute(这是我没有想到的)并使用固定宽度的常量参数:

[self addConstraint:[NSLayoutConstraint constraintWithItem:myButton
      attribute:NSLayoutAttributeWidth 
      relatedBy:NSLayoutRelationEqual 
      toItem:nil 
      attribute:NSLayoutAttributeNotAnAttribute 
      multiplier:1.0 
      constant:200]];

Edit: since this answer still seems to get a fair share of views, I thought I'd add the Swift syntax:

编辑:由于这个答案似乎仍然获得了相当多的观点,我想我会添加 Swift 语法:

self.addConstraint(NSLayoutConstraint(
        item: myButton,
        attribute: .width,
        relatedBy: .equal,
        toItem: nil,
        attribute: .notAnAttribute,
        multiplier: 1.0,
        constant: 200))

回答by Lukas Batteau

How about using Layout Anchors?

如何使用布局锚

myView.widthAnchor.constraintEqualToConstant(29).isActive = true

回答by Eduardo Irias

In swift:

迅速:

let width = 120
let constraint = NSLayoutConstraint(
    item: myView,
    attribute: .width,
    relatedBy: .equal,
    toItem: nil,
    attribute: .notAnAttribute,
    multiplier: 1.0,
    constant: width)
NSLayoutConstraint.activateConstraints([constraint])

Then you can change constraint's constant value

然后你可以改变约束的常量值

constraint.constant = width * 2

回答by Tejvansh

Here's a simple code for button with fixed width.

这是固定宽度按钮的简单代码。

visual format:-

视觉形式:-

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:     [myButton(==50)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(myButton)]];

Use this code for constraint using visual format where self.view is your button's superview and myButton is name of your button and 50 is the width of myButton. You can change these values according to get the desired constraint.

将此代码用于使用视觉格式的约束,其中 self.view 是按钮的超级视图,myButton 是按钮的名称,50 是 myButton 的宽度。您可以根据获得所需的约束来更改这些值。

constraintWithItem format:-

约束带项目格式:-

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:myButton attribute: NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute: NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50.0]];

Use this code for constraint using constraintWithItem format where self.view is your button's superview and myButton is name of your button and 50 is the width of myButton. You can change these values according to get the desired constraint.

将此代码用于使用constraintWithItem 格式的约束,其中self.view 是按钮的超级视图,myButton 是按钮的名称,50 是myButton 的宽度。您可以根据获得所需的约束来更改这些值。

回答by Ashley Mills

Rather than looking for an explicit height (28), a better idea would be to look for a heightconstraint…

与其寻找明确的高度 (28),不如寻找height约束条件……

loginButton.constraints.first(where: { ##代码##.firstAttribute == .height })?.constant = 40