我有一个包含三个文本字段的注册屏幕:一个用于用户名,另外两个用于密码(安全文本输入) . 我已设置侦听器以确定键盘何时出现并消失以相应地移动我的视图,以便键盘不会阻止输入窗口小部件 .
在我的viewDidLoad()中:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
选择器功能:
@objc func keyboardWillShow(notification: NSNotification) {
let userInfo = notification.userInfo
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
keyboardFrame = self.view.convert(keyboardFrame, from: nil)
movement = (keyboardFrame.size.height/2 + 20)
print("Keyboard Appeared with a height of \(movement) including 20 offset")
UIView.animate(withDuration: 0.3, animations: {
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: self.movement * -1.0) //dy is negative for upward movement
})
}
@objc func keyboardWillHide(notification: NSNotification)
print("Keyboard Disappeared with a height of \(movement) offset included")
UIView.animate(withDuration: 0.3, animations: {
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: self.movement)
})
}
并且,将textfield委托设置为视图控制器,我有:
extension SignUpViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == textFieldUsername {
textFieldPassword.becomeFirstResponder()
} else if textField == textFieldPassword {
textFieldPasswordConfirm.becomeFirstResponder()
} else {
textFieldPasswordConfirm.resignFirstResponder()
}
return true
}
}
最后,我还有一个tap taplistener,用于确定用户点击屏幕上的任何位置以消除键盘 . 为此我用过:
view.endEditing(true)
当我点击文本字段时,会调用keyboardWillShow(),当我点击屏幕外的任何地方时,会调用keyboardWillHide() . 这是预期的 .
但是,当我使用返回键从一个文本字段切换到另一个文本字段时,虽然键盘没有从屏幕上消失,但仍会调用keyboardWillShow() .
此外,当我在文本字段中输入一些文本时,随机调用keyboardWillShow() .
如何阻止这些不受欢迎的键盘通知?
EDIT
在阅读了@rmaddy和@Adam Eberbach的答案之后,我想出了不同的方法 . 首先,我为可滚动视图定义一个协议,并在视图控制器中实现它,我希望在其中具有滚动效果:
protocol ScrollableProtocol {
var viewScrolled: Bool { get}
func checkScroll()
func performScroll(direction: Int)
}
然后在视图控制器中:
private var activeTextField: UITextField?
var isScrolled: Bool = false
let offset: CGFloat = 155.00 //for now I'm defining this value as a constant which is supposed to be less than the height of the keyboard
协议的实施:
extension SignUpViewController: ScrollableProtocol {
var viewScrolled: Bool {
get {
return isScrolled
}
}
func checkScroll() {
if !viewScrolled {
performScroll(direction: 0)
isScrolled = !isScrolled
}
}
func registerTapListener() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(clearKeyboard))
view.addGestureRecognizer(tap)
}
@objc func clearKeyboard() {
activeTextField?.resignFirstResponder()
if viewScrolled {
performScroll(direction: 1)
isScrolled = !isScrolled
}
}
func performScroll(direction: Int) {
//0 -> scroll up, 1 -> scroll down
if direction == 0 {
UIView.animate(withDuration: 0.3, animations: {
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: self.offset * -1)
})
} else if direction == 1 {
UIView.animate(withDuration: 0.3, animations: {
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: self.offset)
})
}
}
}
UITextField委托方法:
extension SignUpViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
activeTextField = textField
checkScroll()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == textFieldUsername {
textFieldPassword.becomeFirstResponder()
} else if textField == textFieldPassword {
textFieldPasswordConfirm.becomeFirstResponder()
} else {
textFieldPasswordConfirm.resignFirstResponder()
performScroll(direction: 1)
isScrolled = !isScrolled
}
return true
}
}
最后,viewDidLoad方法:
override func viewDidLoad() {
super.viewDidLoad()
registerTapListener()
textFieldUsername.delegate = self
textFieldPassword.delegate = self
textFieldPasswordConfirm.delegate = self
}
这种方式是对的还是一种复杂的方法?
2 回答
即使您实际上看不到键盘消失并重新出现,更改第一响应者也会导致键盘事件发生 . 每个文本字段可以具有不同的
inputView
或不同的键盘类型 . 每个文本字段可以有不同的inputAccessoryView
. 因此,当每个不同的文本字段成为第一响应者时,隐藏和显示事件应该发生 . 这使您有机会处理不同大小的键盘和附件视图 .您应该在
keyboardWillShow
和keyboardWillHide
中更改动画持续时间以匹配键盘的动画持续时间 . 您可以使用UIKeyboardAnimationDurationUserInfoKey
和UIKeyboardAnimationCurveUserInfoKey
从通知的userInfo
获取此值 .我不相信你能;每当iOS感觉发送给您时,您都会收到通知 . 每次收到视图通知时,您应该使用绝对计算而不是向视图偏移量添加或减去键盘高度 .
通常的过程是找出通知指示可见区域底部的位置,然后找出编辑后的控件必须与之相关的位置,并相应地设置一些约束到动画视图位置,通常具有相同的持续时间在显示/隐藏通知中收到 . 由于您进行的计算(包括控制底边和可见区域底部之间的任何边距)每次对于这些重复通知都会产生相同的数字,因此您不会移动任何内容 . 因此,您的视图始终处于可见位置,并且用户看不到四处移动,因为您应用的约束常量值大部分时间等于现有约束常量值 - 因此不会发生移动 .
当收到键盘显示通知并且之前未显示,或者收到隐藏通知且之前可见,或者键盘高度不同时,则常量值会发生变化,您可以设置视图的动画以适应新的展示安排 .