布局约束不适用于 view.frame.size.height
Layout constraints not working with view.frame.size.height
我正在以编程方式创建我的 UI,我想让我的 UITextfield
topAnchor
以其包含的 UIView
的高度为基础,但它不起作用.
我的 UITextfield
在 UIView
里面,我希望它的 topAnchor
是 UIView
高度的 0.15 - containerView.frame.size.height * 0.15
但是它似乎价值没有回升。所有这些都在 UIView
子类中完成并在 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
您需要使用 self
而不是 container
,因为容器的高度尚未计算
userTextField.topAnchor.constraint(equalTo: containerView.topAnchor,
constant: self.frame.size.height * 0.15 * 0.65 )
鉴于容器的高度 = 0.65 * 此处距自身
self.heightAnchor, multiplier: 0.65
不要混用 NSLayoutConstraint
和 frame
。这些帧是 static 并且当您将它们用于约束中的 constant
时不会更新。因此,当 Auto Layout 使用其他约束来更新 containerView
的高度时,查看 containerView
框架高度的约束将基于containerView
在创建约束时(很可能是 0
)而不是在 Auto Layout 时使用约束来建立 containerView
.
相反,您想将 userTextField
的 top
基于 containerView
的 bottom
和 multiplier
。不幸的是,您不能使用布局锚点来做到这一点,您必须使用 NSLayoutContraint
来创建它:
替换:
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)
我正在以编程方式创建我的 UI,我想让我的 UITextfield
topAnchor
以其包含的 UIView
的高度为基础,但它不起作用.
我的 UITextfield
在 UIView
里面,我希望它的 topAnchor
是 UIView
高度的 0.15 - containerView.frame.size.height * 0.15
但是它似乎价值没有回升。所有这些都在 UIView
子类中完成并在 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
您需要使用 self
而不是 container
,因为容器的高度尚未计算
userTextField.topAnchor.constraint(equalTo: containerView.topAnchor,
constant: self.frame.size.height * 0.15 * 0.65 )
鉴于容器的高度 = 0.65 * 此处距自身
self.heightAnchor, multiplier: 0.65
不要混用 NSLayoutConstraint
和 frame
。这些帧是 static 并且当您将它们用于约束中的 constant
时不会更新。因此,当 Auto Layout 使用其他约束来更新 containerView
的高度时,查看 containerView
框架高度的约束将基于containerView
在创建约束时(很可能是 0
)而不是在 Auto Layout 时使用约束来建立 containerView
.
相反,您想将 userTextField
的 top
基于 containerView
的 bottom
和 multiplier
。不幸的是,您不能使用布局锚点来做到这一点,您必须使用 NSLayoutContraint
来创建它:
替换:
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)