0

There is a strange behavior going on with notifications. My notifications were firing properly whenever I switch one UITextfield to another. But recently I noticed they are not being fired now while switching from one UITextField to another but firing properly when the keyboard is hidden. Has Apple changed this logic?

I need to scroll the field to visible and as the observers are not firing now while switching, the fields don't come to visible rect.

I have created an alternative by posting notification on the textFieldDidBeginEditing, but I would like old way back if it is possible.

Hassy
  • 5,068
  • 5
  • 38
  • 63
  • What code have you tried so far? – ielyamani Oct 04 '18 at 23:13
  • I have tried all available code for swift 4 and swift 4.2 with no avail. I tried using observers with UIResponder (swift 4.2) and with Notification.Name.UIKeyboardWillShow. Same behavior is observed. – Hassy Oct 05 '18 at 05:32

2 Answers2

1

I found the real reason of my issue. Keyboard notifications will not be called while switching if the UITextfield do not have accessory view and in case of accessory view they will be called each time (Why is UIKeyboardWillShowNotification called every time another TextField is selected?)

In my case I had accessory view on each field but while adding accessory view I was adding same view to all of the fields, that was the real problem. I needed to assign separate instance of UIView to different fields. As soon as I did that my problem was gone.

Hassy
  • 5,068
  • 5
  • 38
  • 63
0

I had the exact same problem and I handled it via creating an accessoryView with a new button that is a duplication of my actual button. So basically, consider you have a UIButton which is sticking at the bottom of the UIViewController and you have 2 UITextField which when switched interchangeably, cannot feel that the UIButton was ever moved elsewhere but remain stuck on the keyboard. This following piece of code explains how to cater this problem:

@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),
        ])
}

And you're done. This will keep the UIButton always present on the Keyboard. Important thing is no matter how many instances of UITextField you introduce, always input the accessoryViewKeyboard as its inputAccessoryView.

Mohsin Khubaib Ahmed
  • 1,008
  • 16
  • 32