移动自动布局约束一个文本框的键盘出现时

移动自动布局约束一个文本框的键盘出现时

本文介绍了移动自动布局约束一个文本框的键盘出现时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索栏的文本字段和表视图(谷歌自动完成),我想翻译起来的时候键盘映入眼帘。我成功地这样做,但我得到了我的约束警告/错误。我对这种观点通过故事板使用自动布局,并试图禁用/启用前/后显示/隐藏键盘的限制,但我仍然得到这些错误。我不能正确禁用自动布局?我跟着什么是在this SO回应。

 覆盖FUNC viewDidLoad中(){
    ...
    。NSNotificationCenter.defaultCenter()的addObserver(自我,选择:选择(keyboardWillShow),名称:UIKeyboardWillShowNotification,对象:无)
    。NSNotificationCenter.defaultCenter()的addObserver(自我,选择:选择(keyboardWillHide),名称:UIKeyboardWillHideNotification,对象:无)
    ...
}
FUNC keyboardWillShow(发件人:NSNotification){
    self.pixieLabel.hidden =真
    self.searchBar.setTranslatesAutoresizingMaskIntoConstraints(真)
    self.startingTableView.setTranslatesAutoresizingMaskIntoConstraints(真)
    self.searchBar.frame.origin.y - = 150
    self.startingTableView.frame.origin.y - = 150
}
FUNC keyboardWillHide(发件人:NSNotification){
    self.pixieLabel.hidden = FALSE
    self.searchBar.setTranslatesAutoresizingMaskIntoConstraints(假)
    self.startingTableView.setTranslatesAutoresizingMaskIntoConstraints(假)
    self.searchBar.frame.origin.y + = 150
    self.startingTableView.frame.origin.y + = 150
}

解决方案code

  FUNC keyboardWillShow(发件人:NSNotification){
    self.pixieLabel.hidden =真
    self.seachBarTopConstraint.constant - = 150
    self.searchBar.layoutIfNeeded()
}
FUNC keyboardWillHide(发件人:NSNotification){
    self.pixieLabel.hidden = FALSE
    self.seachBarTopConstraint.constant + = 150
    self.searchBar.layoutIfNeeded()
}


解决方案
除了调整

,我想你应该创建 @ IBOutlet中,并在Interface Builder约束参考文献,然后改变这些约束的值当你想制作动画,随后调用 layoutIfNeeded 。据我了解,手动更改视图的框架和经销商布局的值不混合。

另外,我也不会用 setTranslatesAutoresizingMaskIntoConstraints 除非你是编程方式将您的约束,在这种情况下,你很可能只是将其设置为<$ C $浪费时间C>假。

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
}

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()
}
解决方案

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.

这篇关于移动自动布局约束一个文本框的键盘出现时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:13