键盘出现时,使用自动布局约束移动文本字段 [英] Moving a textfield with auto layout constraints when the keyboard appears

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

问题描述

我有一个搜索栏文本字段和一个表视图(谷歌自动完成),我想翻译当键盘进入视图。我成功地做到这一点,但是,我得到关于我的约束的警告/错误。我使用自动布局通过故事板在这个视图,并试图禁用/启用约束之前/之后显示/隐藏键盘,但我仍然得到这些错误。我没有正确禁用自动布局?我遵循了 this SO中提供的内容

  override func viewDidLoad(){
...
NSNotificationCenter.defaultCenter()。addObserver (self,selector:Selector(keyboardWillHead:),name:UIKeyboardWillHideNotification,object(objectWithShowNotification,object:nil)
NSNotificationCenter.defaultCenter()。addObserver :nil)
...
}
func keyboardWillShow(sender:NSNotification){
self.pixieLabel.hidden = true
self.searchBar.setTranslatesAutoresizingMaskIntoConstraints
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
}

>



解决方案代码



  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()
}


解决方案

而不是调整框架值,我认为你应该创建 @IBOutlet 引用Interface Builder中的约束,然后在想要对它们进行动画处理时更改这些约束的常量值,到 layoutIfNeeded 。根据我的理解,手动更改视图框架和自动布局的值不会混合。



此外,我不会搞乱 setTranslatesAutoresizingMaskIntoConstraints ,除非您以编程方式添加您的约束,在这种情况下,您最有可能将其设置为 false 。 $ b

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆