When I comment out the constraints it works perfectly, but the problem is the height of the textfield
becomes too small, when I add the heightConstraint
, it works fine in my iPhone XS Max
, but crashes in other Iphones...
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
}()
Would you please try this
tf.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 1.0),
instead of
tf.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 1),
tf.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 1)
Try this first, if this doesnot solve your problem then remove bottomanchor
Either use heightConstraint or use topAnchor and bottomAnchor . Don't use both cause xcode will not understand what to do, you are just making it confuses by giving height and autolayout .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.