我有一个按钮,您可以在其中使用键盘获取文本字段。此刻,当我获得此文本字段时,该文本字段位于左上角,并且出现该文本字段。文本框在键盘上方,我该如何处理?
问题是,editTextButtonTapped中的代码完成后,我得到了键盘的高度。在调用editTextButtonTapped之前如何获取键盘的高度?
//这是我用于创建文本字段的全局变量
var myTextField: UITextField = UITextField(frame: CGRect(x: 0,y: 0, width: 0, height:0))
var firstkeyboardHeight = CGFloat()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
@objc func keyboardWillShow(_ notification: NSNotification) -> Void {
if let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue {
let keyboardHeight = keyboardRect.height
firstkeyboardHeight = keyboardHeight
}
}
@IBAction func editTextButtonTapped(_ sender: UIButton) {
myTextField.becomeFirstResponder()
self.myTextField = UITextField(frame: CGRect(x: 0,y: self.firstkeyboardHeight, width: self.view.frame.width, height: 50.0))
self.myTextField.backgroundColor = .green
self.Gestures(addGesture: self.myTextField)
self.view.addSubview(self.myTextField)
}
答案 0 :(得分:1)
只有这组通知:
[ UIResponder.keyboardWillShowNotification, UIResponder.keyboardWillHideNotification, UIResponder.keyboardDidShowNotification, UIResponder.keyboardDidHideNotification]
只有内部可以获取帧值:
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification,
object: nil,
queue: .main) { [weak self] notification in
guard let userInfo = notification.userInfo else {
return
}
let initialValue = userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue,
let finalValue = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue,
let durationValue = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber,
let curveValue = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
}
所以,我认为没有任何可能的允许方式。
答案 1 :(得分:0)
您有几种方法可以做到这一点。无论哪种情况,我都避免每次都重新创建文本字段。
let myTextField: UITextField = {
let textfield = UITextField()
textfield.translatesAutoresizingMaskIntoConstraints = false
textfield.backgroundColor = .green
textfield.isHidden = true
return textfield
}()
var bottomConstraint: NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(myTextField)
let bottomConstraint = myTextField.bottomAnchor.constraint(equalTo: view.bottomAnchor)
NSLayoutConstraint.activate([
myTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor),
myTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor),
myTextField.heightAnchor.constraint(equalToConstant: 50),
bottomConstraint
])
self.bottomConstraint = bottomConstraint
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}
@objc func keyboardWillShow(_ notification: NSNotification) -> Void {
guard let keyboardFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
bottomConstraint?.constant = -keyboardFrame.height
}
@IBAction func editTextButtonTapped(_ sender: UIButton) {
myTextField.becomeFirstResponder()
myTextField.isHidden = false
}