布局约束不适用于 view.frame.size.height [英] Layout constraints not working with view.frame.size.height

查看:49
本文介绍了布局约束不适用于 view.frame.size.height的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以编程方式创建我的 UI,我想让我的 UITextfield topAnchor 基于它包含的 UIView 的高度但是它不起作用.

I'm creating my UI programmatically and I want to make my UITextfield topAnchor to be based off the height of its containing UIView but it's not working.

我的 UITextfieldUIView 内,我希望它的 topAnchorUIView 高度的 0.15因此 - containerView.frame.size.height * 0.15 但似乎价值没有上升.所有这些都在 UIView 子类中完成并在 ViewController

My UITextfield is inside a UIView and I want its topAnchor to be 0.15 the height of the UIView as such - containerView.frame.size.height * 0.15 but it seems the value is not picking up. All this is done in a UIView subclass and called in the ViewController

        let containerView: UIView
        containerView.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(containerView)

        NSLayoutConstraint.activate([
            containerView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
            containerView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.9),
            containerView.topAnchor.constraint(equalTo: stepTitleLabel.bottomAnchor, constant: 50),
            containerView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.65)
            ])


        let userTextField: UITextField
        userTextField?.tag = 1
        userTextField?.translatesAutoresizingMaskIntoConstraints = false
        containerView.addSubview(userTextField)

        userTextField.setNeedsLayout()
        print("containerView.frame.size.height * 0.15:\(containerView.frame.size.height * 0.15)")
        NSLayoutConstraint.activate([
            (userTextField.centerXAnchor.constraint(equalTo: containerView.centerXAnchor))!,
            (userTextField.widthAnchor.constraint(equalTo: containerView.widthAnchor, multiplier: 0.8))!,
            userTextField.topAnchor.constraint(equalTo: containerView.topAnchor, constant: containerView.frame.size.height * 0.15),
            userTextField.heightAnchor.constraint(equalToConstant: 40)
            ])

我希望我的 UITextField 的 topAnchor 在所有设备上看起来都相似,其容器视图的高度为 0.15,但我得到的是 0

I expect the topAnchor of my UITextField to appear similar across all devices at 0.15 the height of it container view but what I'm getting is 0

推荐答案

不要混用 NSLayoutConstraintframe.框架是静态,当您将它们用于约束中的 constant 时不会更新.因此,当 Auto Layout 使用其他约束来更新 containerView 的高度时,查看 containerView 框架高度的约束将基于containerView 在约束创建时的高度(可能是 0)而不是在 Auto Layout 使用约束来设置containerView的高度.

Don't mix NSLayoutConstraints and frames. The frames are static and don't update when you use them for the constant in a constraint. So when Auto Layout uses the other constraints to update the height of containerView, your constraint that looks at containerView's frame height will be based upon the height of the containerView at the time of the constraint's creation (which is likely 0) and not at the time Auto Layout uses the constraints to establish the height of containerView.

相反,您希望将 userTextFieldtop 基于 containerViewbottom>乘数.不幸的是,你不能用布局锚来做到这一点,你必须使用 NSLayoutContraint 来创建它:

Instead, you want to base the top of userTextField on the bottom of containerView with a multiplier. Unfortunately, you can't do that with layout anchors, and you'll have to use NSLayoutContraint to create it:

替换:

userTextField.topAnchor.constraint(equalTo: containerView.topAnchor,
    constant: containerView.frame.size.height * 0.15)

与:

NSLayoutConstraint(item: userTextField, attribute: .top, relatedBy: .equal,
    toItem: containerView, attribute: .bottom, multiplier: 0.15, constant: 0)

这篇关于布局约束不适用于 view.frame.size.height的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆