0

I have a UITextField (searchField) at the center of my viewcontroller and I'm creating a thin line by using UIView (liveView). I create the lineView and its constraints programmatically. The lineView is essentially an underline that grows in width beneath the textField as the user types. Once the user presses enter, I'm adjusting the constraints so the textfield and the view move to the top of the viewcontroller. I got this working, but as the lineView is moving up, it starts fading out and disappears. No idea why this is happening.

 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


    })

}

When I checkout the view hierarchy, the view is nowhere to be found.

Brosef
  • 2,945
  • 6
  • 34
  • 69

1 Answers1

0

After lots of debugging, I decided to increase the size of the view to get a better idea of what was happening. It became clear that the height was shrinking. All I had to do was set a height constraint and everything worked.

let heightConstraint = NSLayoutConstraint(item: lineView!, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: (lineView?.frame.size.height)!)
Brosef
  • 2,945
  • 6
  • 34
  • 69
  • FWIW, I would just constrain the `lineView`'s `leading` and `trailing` edges to the text field's edges, and constrain the `lineView`'s height. Now it will stay in sync with the text field no matter how it's animated. – NRitH Jun 21 '17 at 17:37