iOS - 切换 UITextfields 时不触发键盘通知
iOS - Keyboard Notifications not firing while switching UITextfields
通知发生了奇怪的行为。每当我将一个 UITextfield
切换到另一个时,我的通知都会正常触发。但最近我注意到,当从一个 UITextField
切换到另一个时,它们现在并没有被触发,而是在隐藏键盘时正确触发。 Apple 改变了这个逻辑吗?
我需要将字段滚动到可见,并且由于 观察者 在切换时现在没有开火,字段不会变成可见的矩形。
我已经通过在 textFieldDidBeginEditing
上发布通知创建了一个替代方案,但如果可能的话,我希望返回原路。
我找到了问题的真正原因。如果 UITextfield
没有附件视图,则切换时不会调用键盘通知,如果有附件视图,则每次都会调用它们 (Why is UIKeyboardWillShowNotification called every time another TextField is selected?)
在我的例子中,我在每个字段上都有附件视图,但是在添加附件视图时,我向所有字段添加了相同的视图,这才是真正的问题。我需要将单独的 UIView 实例分配给不同的字段。一旦我这样做了,我的问题就消失了。
我遇到了完全相同的问题,我通过创建一个带有新按钮的 accessoryView 来处理它,该按钮是我实际按钮的副本。所以基本上,假设你有一个 UIButton
,它粘在 UIViewController
的底部,你有 2 个 UITextField
,当交替切换时,感觉不到 UIButton
曾经移动到别处但仍停留在键盘上。下面这段代码解释了如何解决这个问题:
@IBOutlet weak var signInBtn: UIButton!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
var accessoryViewKeyboard:UIView?
var btnAccessory:UIButton?
override func viewDidLoad() {
super.viewDidLoad()
passwordTextField.delegate = self
emailTextField.delegate = self
accessoryViewKeyboard = UIView(frame: signInBtn.frame)
//Inputting the "accessoryViewKeyboard" here as the "inputAccessoryView" is of
//utmost importance to help the "signInBtn" to show up on tap of different "UITextFields"
emailTextField.inputAccessoryView = accessoryViewKeyboard
passwordTextField.inputAccessoryView = accessoryViewKeyboard
setupBtnWithKeyboard()
}
func setupBtnWithKeyboard() {
btnAccessory = UIButton(frame: CGRect(x: signInBtn.frame.origin.x, y: signInBtn.frame.origin.y, width: self.view.frame.size.width, height: signInBtn.frame.size.height))
accessoryViewKeyboard?.addSubview(btnAccessory!)
btnAccessory?.translatesAutoresizingMaskIntoConstraints = false
btnAccessory?.frame = CGRect(x: (accessoryViewKeyboard?.frame.origin.x)!,
y: (accessoryViewKeyboard?.frame.origin.y)!,
width: self.view.frame.size.width,
height: (accessoryViewKeyboard?.frame.size.height)!)
btnAccessory?.backgroundColor = UIColor(red: 31/255, green: 33/255, blue: 108/255, alpha: 1)
btnAccessory?.setTitle("Sign In", for: .normal)
btnAccessory?.titleLabel?.font = .systemFont(ofSize: 22)
btnAccessory?.titleLabel?.textColor = UIColor.white
btnAccessory?.titleLabel?.textAlignment = .center
btnAccessory?.isEnabled = true
btnAccessory?.addTarget(self, action: #selector(SignIn.signInBtnPressed), for: .touchUpInside)
NSLayoutConstraint.activate([
btnAccessory!.leadingAnchor.constraint(equalTo:
accessoryViewKeyboard!.leadingAnchor, constant: 0),
btnAccessory!.centerYAnchor.constraint(equalTo:
accessoryViewKeyboard!.centerYAnchor),
btnAccessory!.trailingAnchor.constraint(equalTo:
accessoryViewKeyboard!.trailingAnchor, constant: 0),
btnAccessory!.heightAnchor.constraint(equalToConstant: signInBtn.frame.size.height),
])
}
大功告成。这将使 UIButton
始终出现在键盘上。重要的是无论你引入多少个 UITextField
实例,总是输入 accessoryViewKeyboard
作为它的 inputAccessoryView
.
通知发生了奇怪的行为。每当我将一个 UITextfield
切换到另一个时,我的通知都会正常触发。但最近我注意到,当从一个 UITextField
切换到另一个时,它们现在并没有被触发,而是在隐藏键盘时正确触发。 Apple 改变了这个逻辑吗?
我需要将字段滚动到可见,并且由于 观察者 在切换时现在没有开火,字段不会变成可见的矩形。
我已经通过在 textFieldDidBeginEditing
上发布通知创建了一个替代方案,但如果可能的话,我希望返回原路。
我找到了问题的真正原因。如果 UITextfield
没有附件视图,则切换时不会调用键盘通知,如果有附件视图,则每次都会调用它们 (Why is UIKeyboardWillShowNotification called every time another TextField is selected?)
在我的例子中,我在每个字段上都有附件视图,但是在添加附件视图时,我向所有字段添加了相同的视图,这才是真正的问题。我需要将单独的 UIView 实例分配给不同的字段。一旦我这样做了,我的问题就消失了。
我遇到了完全相同的问题,我通过创建一个带有新按钮的 accessoryView 来处理它,该按钮是我实际按钮的副本。所以基本上,假设你有一个 UIButton
,它粘在 UIViewController
的底部,你有 2 个 UITextField
,当交替切换时,感觉不到 UIButton
曾经移动到别处但仍停留在键盘上。下面这段代码解释了如何解决这个问题:
@IBOutlet weak var signInBtn: UIButton!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
var accessoryViewKeyboard:UIView?
var btnAccessory:UIButton?
override func viewDidLoad() {
super.viewDidLoad()
passwordTextField.delegate = self
emailTextField.delegate = self
accessoryViewKeyboard = UIView(frame: signInBtn.frame)
//Inputting the "accessoryViewKeyboard" here as the "inputAccessoryView" is of
//utmost importance to help the "signInBtn" to show up on tap of different "UITextFields"
emailTextField.inputAccessoryView = accessoryViewKeyboard
passwordTextField.inputAccessoryView = accessoryViewKeyboard
setupBtnWithKeyboard()
}
func setupBtnWithKeyboard() {
btnAccessory = UIButton(frame: CGRect(x: signInBtn.frame.origin.x, y: signInBtn.frame.origin.y, width: self.view.frame.size.width, height: signInBtn.frame.size.height))
accessoryViewKeyboard?.addSubview(btnAccessory!)
btnAccessory?.translatesAutoresizingMaskIntoConstraints = false
btnAccessory?.frame = CGRect(x: (accessoryViewKeyboard?.frame.origin.x)!,
y: (accessoryViewKeyboard?.frame.origin.y)!,
width: self.view.frame.size.width,
height: (accessoryViewKeyboard?.frame.size.height)!)
btnAccessory?.backgroundColor = UIColor(red: 31/255, green: 33/255, blue: 108/255, alpha: 1)
btnAccessory?.setTitle("Sign In", for: .normal)
btnAccessory?.titleLabel?.font = .systemFont(ofSize: 22)
btnAccessory?.titleLabel?.textColor = UIColor.white
btnAccessory?.titleLabel?.textAlignment = .center
btnAccessory?.isEnabled = true
btnAccessory?.addTarget(self, action: #selector(SignIn.signInBtnPressed), for: .touchUpInside)
NSLayoutConstraint.activate([
btnAccessory!.leadingAnchor.constraint(equalTo:
accessoryViewKeyboard!.leadingAnchor, constant: 0),
btnAccessory!.centerYAnchor.constraint(equalTo:
accessoryViewKeyboard!.centerYAnchor),
btnAccessory!.trailingAnchor.constraint(equalTo:
accessoryViewKeyboard!.trailingAnchor, constant: 0),
btnAccessory!.heightAnchor.constraint(equalToConstant: signInBtn.frame.size.height),
])
}
大功告成。这将使 UIButton
始终出现在键盘上。重要的是无论你引入多少个 UITextField
实例,总是输入 accessoryViewKeyboard
作为它的 inputAccessoryView
.