【问题标题】:iOS - Keyboard Notifications not firing while switching UITextfieldsiOS - 切换 UITextfields 时未触发键盘通知
【发布时间】:2019-03-09 20:54:53
【问题描述】:

通知出现了一种奇怪的行为。每当我将一个 UITextfield 切换到另一个时,我的通知都会正常触发。但最近我注意到,当从一个UITextField 切换到另一个时,它们现在没有被解雇,而是在隐藏键盘时正确发射。 Apple 改变了这个逻辑吗?

我需要将字段滚动到可见,并且由于 观察者 在切换时现在没有触发,因此字段不会变为可见矩形。

我已经通过在textFieldDidBeginEditing 上发布通知创建了一个替代方案,但如果可能的话,我希望回到旧的方式。

【问题讨论】:

  • 到目前为止你尝试过什么代码?
  • 我已经尝试了 swift 4 和 swift 4.2 的所有可用代码,但无济于事。我尝试使用带有 UIResponder (swift 4.2) 和 Notification.Name.UIKeyboardWillShow 的观察者。观察到相同的行为。

标签: ios nsnotifications xcode10 ios12 swift4.2


【解决方案1】:

我找到了问题的真正原因。如果UITextfield没有附件视图,则切换时不会调用键盘通知,如果是附件视图,则每次都会调用它们(Why is UIKeyboardWillShowNotification called every time another TextField is selected?

在我的情况下,我在每个字段上都有附件视图,但是在添加附件视图时,我向所有字段添加了相同的视图,这是真正的问题。我需要将单独的 UIView 实例分配给不同的字段。一旦我这样做了,我的问题就消失了。

【讨论】:

    【解决方案2】:

    我遇到了完全相同的问题,我通过创建一个带有一个新按钮的附件视图来处理它,该按钮是我实际按钮的副本。所以基本上,考虑你有一个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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-22
      • 2018-03-10
      • 2018-10-30
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      相关资源
      最近更新 更多