出现奇怪的自动布局错误

时间:2017-09-03 18:08:35

标签: ios swift autolayout swift4

我认为我必须使用此功能隐藏和调整大小

@objc func dismiss() {
        self.endEditing(true)
        self.isHidden = true

        NewTodoReference.newTodo?.detailsHeightAnchor?.isActive = false
        NewTodoReference.newTodo?.detailsHeightAnchor = NewTodoReference.newTodo?.detailsPopUp.heightAnchor.constraint(equalToConstant: 46)
        NewTodoReference.newTodo?.detailsHeightAnchor?.isActive = true

        UIView.animate(withDuration: 0.3) {
            NewTodoReference.newTodo?.layoutIfNeeded()
        }
}

唯一的问题是我从调试器中得到了这个自动布局问题:

"<NSLayoutConstraint:0x608000286e00 Hippotomo.NewTodoView:0x7ffd6a008c30.height == UIView:0x7ffd68506220.height - 69   (active)>",
    "<NSLayoutConstraint:0x60c00009d4c0 Hippotomo.WhiteMenuPopUp:0x7ffd6a300c80.height == Hippotomo.NewTodoView:0x7ffd6a008c30.height   (active)>",
    "<NSLayoutConstraint:0x60c000288d40 Hippotomo.WhiteMenuPopUp:0x7ffd6a300c80.height == 46   (active)>",
    "<NSLayoutConstraint:0x60c000283bb0 'UIView-Encapsulated-Layout-Height' UIView:0x7ffd68506220.height == 736   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60c000288d40 Hippotomo.WhiteMenuPopUp:0x7ffd6a300c80.height == 46   (active)>

这是我创建的所有约束,我必须调整大小

var detailsYAnchor: NSLayoutConstraint?
var detailsWidthAnchor: NSLayoutConstraint?
var detailsHeightAnchor: NSLayoutConstraint?

这是原始约束:

detailsPopUp.centerXAnchor.constraint(equalTo: centerXAnchor).isActive
= true

detailsYAnchor = detailsPopUp.topAnchor.constraint(equalTo: activityTextfield.bottomAnchor, constant: 17)
detailsYAnchor?.isActive = true

detailsWidthAnchor = detailsPopUp.widthAnchor.constraint(equalTo: widthAnchor, constant:
-32)
detailsWidthAnchor?.isActive = true

detailsHeightAnchor = detailsPopUp.heightAnchor.constraint(equalToConstant: 46)
detailsHeightAnchor?.isActive = true

这是让我的视野更大的功能:

func showPopUps(view: UIView, y: NSLayoutConstraint, width: NSLayoutConstraint, height: NSLayoutConstraint) {

        var y = y
        var width = width
        var height = height

        y.isActive = false
        y = view.centerYAnchor.constraint(equalTo: centerYAnchor)
        y.isActive = true
        width.isActive = false
        width = view.widthAnchor.constraint(equalTo: widthAnchor)
        width.isActive = true
        height.isActive = false
        height = view.heightAnchor.constraint(equalTo: heightAnchor)
        height.isActive = true

        UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.9, options: .curveEaseOut, animations: {
            self.layoutIfNeeded()
            self.updateConstraintsIfNeeded()
        }, completion: nil)
}

@objc func presentDetailsPopUp() {
        detailsPopUp.isHidden = false
        showPopUps(view: detailsPopUp, y: detailsYAnchor!, width: detailsWidthAnchor!, height: detailsHeightAnchor!)
        detailsPopUp.textView.becomeFirstResponder()
        detailsPopUp.presentTextView()
    }

有人能帮助我吗?

0 个答案:

没有答案