出现键盘时移动具有自动布局约束的文本字段

Moving a textfield with auto layout constraints when the keyboard appears(出现键盘时移动具有自动布局约束的文本字段)

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

问题描述

我有一个搜索栏文本字段和一个表格视图(用于谷歌自动完成),当键盘进入视图时,我想翻译它们.我成功地做到了这一点,但是,我收到了关于我的约束的警告/错误.我在此视图上通过情节提要使用自动布局,并尝试在显示/隐藏键盘之前/之后禁用/启用约束,但我仍然收到这些错误.我没有正确禁用自动布局吗?我遵循了 this SO回应.

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
}

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 对约束的引用,而不是调整 frame 值Interface Builder,然后在您想要为它们设置动画时更改这些约束的 constant 值,然后调用 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.

另外,我不会乱用 setTranslatesAutoresizingMaskIntoConstraints,除非您以编程方式添加约束,在这种情况下,您很可能只是将其设置为 false.

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.

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

本文标题为:出现键盘时移动具有自动布局约束的文本字段

基础教程推荐