以编程方式更新高度约束

时间:2015-05-05 18:17:11

标签: ios objective-c swift autolayout nslayoutconstraint

我是自动布局的新手。我已经完成了xib文件中的所有项目,但现在我遇到了一个问题,我必须以编程方式更新视图的高度。我已尝试过,但现在正在努力。

[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:loginContainer attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:loginFrame.size.height]];

在控制台中显示

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) 
(
    "<NSLayoutConstraint:0x78724530 V:[UIView:0x790cdfb0(170)]>",
    "<NSLayoutConstraint:0x787da210 V:[UIView:0x790cdfb0(400)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x78724530 V:[UIView:0x790cdfb0(170)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

5 个答案:

答案 0 :(得分:106)

您需要修改现有约束的常量,而不是添加新约束。

使用IBOutlet连接到Interface Builder中的约束:

@property (nonatomic, weak) NSLayoutConstraint *heightConstraint;

然后,当您需要以编程方式设置它时,只需在约束上设置常量属性:

heightConstraint.constant = 100;

OR

如果无法在Interface Builder中访问nib,请在代码中找到约束:

NSLayoutConstraint *heightConstraint;
for (NSLayoutConstraint *constraint in myView.constraints) {
    if (constraint.firstAttribute == NSLayoutAttributeHeight) {
        heightConstraint = constraint;
        break;
    }
}
heightConstraint.constant = 100;

在斯威夫特:

if let constraint = (myView.constraints.filter{$0.firstAttribute == .width}.first) {
            constraint.constant = 100.0
        }

答案 1 :(得分:20)

要获得身高限制的参考: 单击约束中的+ Ctrl并拖放到类文件中:

enter image description here

更新约束值:

self.heightConstraint.constant = 300;
[self.view updateConstraints];

答案 2 :(得分:7)

使用此快速扩展程序的更灵活方式:

extension UIView {    

    func updateConstraint(attribute: NSLayoutAttribute, constant: CGFloat) -> Void {
        if let constraint = (self.constraints.filter{$0.firstAttribute == attribute}.first) {
            constraint.constant = constant
            self.layoutIfNeeded()
        }
    }
}

如何使用此视图扩展来更新现有约束的常量值:

// to update height constant
testView.updateConstraint(attribute: NSLayoutAttribute.height, constant: 20.0)


// to update width constant
testView.updateConstraint(attribute: NSLayoutAttribute.width, constant: 20.0)

答案 3 :(得分:6)

让我们调用您的视图nibView。因此,您尝试在视图控制器中加载该视图,因此首先,在视图控制器中,您需要在执行此操作时加载它:

[[NSBundle mainBundle] loadNibNamed:@"NibView" owner:self options:nil];

然后你需要告诉nibView你不需要它通过做

将自动调整掩码变换为约束
nibView.translatesAutoresizingMaskIntoConstraints = NO;

然后,您可以添加约束

    NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:nibView
                                                                                  attribute:NSLayoutAttributeHeight
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:nil
                                                                                  attribute:NSLayoutAttributeNotAnAttribute
                                                                                 multiplier:1.0
                                                                                   constant:yourValue];

最后只需将其添加到您的视图约束中:

[self.view addConstraint:heightConstraint];

您可能还需要添加宽度约束。

答案 4 :(得分:0)

Swift 4.2

使用自动布局和动画来增加按钮的高度

我不使用故事板。代码中的所有内容。

import UIKit

class AnimateHeightConstraintViewController: UIViewController {
    var flowHeightConstraint: NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        view.addSubview(button)
        button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        button.widthAnchor.constraint(equalToConstant: 100).isActive = true
        flowHeightConstraint = button.heightAnchor.constraint(equalToConstant: 30)
        flowHeightConstraint?.isActive = true
    }

    @objc func animateButtonTapped() {
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseOut, animations: {
            self.flowHeightConstraint?.constant = 100
            self.view.layoutIfNeeded()
        }, completion: nil)
    }

    lazy var button: UIButton = {
       let button = UIButton()
        button.translatesAutoresizingMaskIntoConstraints = false
        button.backgroundColor = .green
        button.addTarget(self, action: #selector(animateButtonTapped), for: .touchUpInside)
        return button
    }()
}

结果是这样的:

enter image description here