以编程方式更新高度约束

Tap*_*Pal 50 objective-c ios autolayout nslayoutconstraint swift

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

[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:loginContainer attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:loginFrame.size.height]];
Run Code Online (Sandbox Code Playgroud)

在控制台中显示

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.
Run Code Online (Sandbox Code Playgroud)

小智 111

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

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

@property (nonatomic, weak) NSLayoutConstraint *heightConstraint;
Run Code Online (Sandbox Code Playgroud)

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

heightConstraint.constant = 100;
Run Code Online (Sandbox Code Playgroud)

要么

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

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

在斯威夫特:

if let constraint = (myView.constraints.filter{$0.firstAttribute == .width}.first) {
            constraint.constant = 100.0
        }
Run Code Online (Sandbox Code Playgroud)

  • 注意.条件是不够的,它应该是constraint.firstAttribute == NSLayoutAttributeHeight && constraint.secondAttribute == NSLayoutAttributeNotAnAttribute排除获得比约束 (4认同)

Ham*_*mza 21

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

在此输入图像描述

要更新约束值:

self.heightConstraint.constant = 300;
[self.view updateConstraints];
Run Code Online (Sandbox Code Playgroud)


Kru*_*nal 9

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

extension UIView {    

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

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

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


// to update width constant
testView.updateConstraint(attribute: NSLayoutAttribute.width, constant: 20.0)
Run Code Online (Sandbox Code Playgroud)


小智 9

斯威夫特 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
    }()
}
Run Code Online (Sandbox Code Playgroud)

结果是这样的:

在此处输入图片说明


fdi*_*iaz 6

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

[[NSBundle mainBundle] loadNibNamed:@"NibView" owner:self options:nil];
Run Code Online (Sandbox Code Playgroud)

然后,您需要告诉nibView您不需要它来通过执行将自动调整掩码转换为约束

nibView.translatesAutoresizingMaskIntoConstraints = NO;
Run Code Online (Sandbox Code Playgroud)

然后,您可以添加约束

    NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:nibView
                                                                                  attribute:NSLayoutAttributeHeight
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:nil
                                                                                  attribute:NSLayoutAttributeNotAnAttribute
                                                                                 multiplier:1.0
                                                                                   constant:yourValue];
Run Code Online (Sandbox Code Playgroud)

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

[self.view addConstraint:heightConstraint];
Run Code Online (Sandbox Code Playgroud)

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