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
.
这篇关于出现键盘时移动具有自动布局约束的文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:出现键盘时移动具有自动布局约束的文本字段
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01