【发布时间】:2018-03-05 18:45:13
【问题描述】:
我有一个UITextField,我正试图在键盘出现时改变它的位置。更具体地说,我想将文本字段向上移动,使其位于键盘上方。我的代码看起来像这样
let textField = myCustomTextField()
override func viewDidLoad() {
//set up textfield here
//constrains blah blah
textField.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor,
constant: -view.bounds.height * 0.05).isActive = true
//more constraints
}
我接下来要做的是更改该约束,以便在键盘出现时提升文本字段。我的代码如下所示:
@objc func keyboardWillShow(notification: NSNotification) {
textField.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor,
constant: -view.bounds.height * 0.05).constant = 200 //200 is a sample number I have a math calculation there
textField.layoutIfNeeded()
}
这不起作用,因为仅使用 constraint(equalTo: constant:) 引用该约束实际上不会返回任何约束。有什么方法可以引用该约束,而无需为我要更改的每个约束创建变量并更改其常量?
【问题讨论】:
标签: swift uitextfield ios-autolayout