0

I have a search bar text field and a table view (for google auto complete) that I would like to translate up when the keyboard comes into view. I am successfully doing this, however, I am getting warnings/errors about my constraints. I am using auto layout via storyboard on this view and tried to disable/enable the constraints prior to/after showing/hiding the keyboard, but I am still getting these errors. Am I not disabling auto layout correctly? I followed what was given in this SO response.

override func viewDidLoad() {
    ...
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil)
    ...
}
func keyboardWillShow(sender: NSNotification) {
    self.pixieLabel.hidden = true
    self.searchBar.setTranslatesAutoresizingMaskIntoConstraints(true)
    self.startingTableView.setTranslatesAutoresizingMaskIntoConstraints(true)
    self.searchBar.frame.origin.y -= 150
    self.startingTableView.frame.origin.y -= 150
}
func keyboardWillHide(sender: NSNotification) {
    self.pixieLabel.hidden = false
    self.searchBar.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.startingTableView.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.searchBar.frame.origin.y += 150
    self.startingTableView.frame.origin.y += 150
}

enter image description here

Solution Code

func keyboardWillShow(sender: NSNotification) {
    self.pixieLabel.hidden = true
    self.seachBarTopConstraint.constant -= 150
    self.searchBar.layoutIfNeeded()
}
func keyboardWillHide(sender: NSNotification) {
    self.pixieLabel.hidden = false
    self.seachBarTopConstraint.constant += 150
    self.searchBar.layoutIfNeeded()
}
Community
  • 1
  • 1
Mike
  • 1,307
  • 3
  • 17
  • 29
  • Have you done your constraints programmatically or in interface builder? – Patrick Lynch Jun 06 '15 at 21:30
  • Apologies, forgot to hit save when I added my code in the edit. Sounds like you didn't have your coffee today @matt. Haha calm down dude. Patrick, my constraints were done in storyboard. – Mike Jun 06 '15 at 21:35

1 Answers1

3

Instead of adjusting theframe values, I think you should be creating @IBOutlet references to constraints in Interface Builder and then changing the constant value of those constraints when you want to animate them, followed by a call to layoutIfNeeded. As I understand it, manually changing the values of a view's frame and auto layout don't mix.

Also, I wouldn't mess around with setTranslatesAutoresizingMaskIntoConstraints unless you are adding your constraints programmatically, in which case you're most likely just setting it to false.

Patrick Lynch
  • 2,742
  • 1
  • 16
  • 18
  • Perfect, this solves my issue of constraint errors/warnings. For some reason though, it takes two taps before the search bar moves. The first tap does nothing, as if it were not registered, then the second tap activates the keyboard and moves the search bar. Is this a constraint bug? – Mike Jun 06 '15 at 21:53