通知发生了奇怪的行为。每当我将一个UITextfield
切换到另一个时,我的通知就会正常启动。但最近我注意到,当他们从一个UITextField
切换到另一个textFieldDidBeginEditing
时,他们并没有被解雇,但是当键盘被隐藏时正常射击。 Apple改变了这种逻辑吗?
我需要将该字段滚动到可见状态,并且由于观察者在切换时没有现在开火,因此字段不会显示为可见矩形。
我已经通过在UITextfield
上发布通知创建了一个替代方案,但是如果有可能的话我想回到过去。
我找到了问题的真正原因。如果Why is UIKeyboardWillShowNotification called every time another TextField is selected?没有附件视图,则在切换时不会调用键盘通知;如果是附件视图,则每次都会调用它们(UIButton
)
在我的情况下,我在每个字段上都有附件视图,但在添加附件视图时,我正在为所有字段添加相同的视图,这才是真正的问题。我需要将不同的UIView实例分配给不同的字段。我一做到这一点,我的问题就消失了。
我遇到了完全相同的问题,我通过创建一个带有新按钮的accessoryView来处理它,该按钮是我实际按钮的重复。所以基本上,考虑你有一个UIViewController
坚持在UITextField
的底部,你有2 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
作为其qazxswpoi。