我的应用程序因高度而崩溃我不知道如何解决它



当我注释掉约束时,它可以完美运行,但问题是textfield的高度变得太小,当我添加heightConstraint时,它在我的iPhone XS Max中工作正常,但在其他 iPhone 中崩溃......

lazy var textField: MaterialTexField = { //MaterialTexField
let tf = MaterialTexField()
tf.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(tf)
NSLayoutConstraint.activate([
tf.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant:16.0),
tf.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant:-16.0),
tf.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant:8.0),
tf.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 1),
//This is the problematic constraints
tf.heightAnchor.constraint(lessThanOrEqualTo:self.contentView.heightAnchor, multiplier:0.9)
return tf
}()

请你试试这个

tf.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 1.0),

而不是

tf.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 1),
tf.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 1)

首先尝试此操作,如果这不能解决您的问题,请删除底部锚

点使用heightConstraint或使用topAnchor 和 bottomAnchor。不要同时使用两者,因为 xcode 不会明白该怎么做,你只是通过给出高度和自动布局来混淆它。

最新更新