Just a coder
Just a coder

Reputation: 16730

inputAccessoryView is stuck at bottom of screen

I have already looked at this solution. I have created a sample project showing the bug.

The view has only one UITextField on it.

and here is my code:

class ViewController: UIViewController {

    @IBOutlet weak var mm: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        let x = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 40))
        x.backgroundColor = .red
        mm.inputAccessoryView = x
    }
}

When I tap on the textfield, here is what happens: --> The red view appears at the bottom and is just stuck there no matter if the keyboard is dismissed or not. Any help please?

enter image description here

Upvotes: 0

Views: 265

Answers (1)

Ahemadabbas Vagh
Ahemadabbas Vagh

Reputation: 494

You are just toggling the keyboard you are not actually dismissing the keyboard try to dismiss the keyboard.

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var textField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        textField.delegate = self
        let view = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 40))
        view.backgroundColor = .red
        textField.inputAccessoryView = view
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
}

enter image description here

Upvotes: 2

Related Questions