【问题标题】:My App crashes because of the heightConstraints I don't know how to fix it由于 heightConstraints 我的应用程序崩溃了我不知道如何解决它
【发布时间】:2019-09-11 07:49:36
【问题描述】:

当我注释掉约束时它工作得很好,但问题是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

}()

【问题讨论】:

  • 你忘了constant:
  • 约束冲突 tf.bottomAnchortf.heightAnchor。尝试仅使用高度约束。看看常数值,附近的乘数。
  • "但在其他 Iphone 中崩溃了..." 也许一些崩溃日志可以帮助...

标签: ios swift nslayoutconstraint


【解决方案1】:

你可以试试这个

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

而不是

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

【讨论】:

  • 这就是为什么我不喜欢以编程方式进行约束......我已经尝试解决这个问题 2 天......
  • 我的......它工作......我注释掉高度约束然后使用它工作的代码。非常感谢。
【解决方案2】:
tf.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 1)

先试试这个,如果这不能解决你的问题,然后删除 bottomanchor

使用 heightConstraint 或使用 topAnchor 和 bottomAnchor。不要同时使用这两种方法,因为 xcode 不知道该怎么做,你只是通过给出 height 和 autolayout 来混淆它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多