【问题标题】:UIView disappearing after adjusting constraint and animating调整约束和动画后UIView消失
【发布时间】:2017-11-23 20:20:23
【问题描述】:

我的视图控制器中心有一个UITextField (searchField),我正在使用UIView (liveView) 创建一条细线。我以编程方式创建 lineView 及其约束。 lineView 本质上是一个下划线,当用户键入时,它在 textField 下方的宽度会增加。一旦用户按下回车,我正在调整约束,以便文本字段和视图移动到视图控制器的顶部。我得到了这个工作,但随着 lineView 向上移动,它开始淡出并消失。不知道为什么会这样。

 func moveUp(){

    verticalConstraint = NSLayoutConstraint(item: lineView!, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: searchField, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 6)

    lineView!.translatesAutoresizingMaskIntoConstraints = false

    let xConstraint = NSLayoutConstraint(item: lineView!, attribute: .centerX, relatedBy: .equal, toItem: searchField, attribute: .centerX, multiplier: 1, constant: 0)

    let widthConstraint = NSLayoutConstraint(item: lineView!, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: (lineView?.frame.size.width)!)

    view.addConstraints([verticalConstraint!, xConstraint, widthConstraint])

    searchBarTopConstraint.constant = 0

    UIView.animate(withDuration: 2.00, delay: 0.0, options: [], animations: {

        self.view.layoutIfNeeded()

    }, completion: { (finished: Bool) in


    })

}

当我检查视图层次结构时,找不到该视图。

【问题讨论】:

    标签: ios uiview nslayoutconstraint


    【解决方案1】:

    经过大量调试后,我决定增加视图的大小以更好地了解正在发生的事情。很明显,高度正在缩小。我所要做的就是设置一个高度限制,一切正常。

    let heightConstraint = NSLayoutConstraint(item: lineView!, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: (lineView?.frame.size.height)!)
    

    【讨论】:

    • FWIW,我只会将lineViewleadingtrailing 边缘限制在文本字段的边缘,并限制lineView 的高度。现在无论动画如何,它都会与文本字段保持同步。
    猜你喜欢
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 2019-06-05
    • 2015-09-13
    • 2013-07-11
    相关资源
    最近更新 更多