Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

ios - Moving a textfield with auto layout constraints when the keyboard appears

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()
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...