frameworker
frameworker

Reputation: 412

replacing UIBarButtonItem with custom view

Found this code to insert the "Done" Button within the decimal keyboard pad and it works as long I don't use a custom view for the button like in this piece of code:

extension UITextField {
    func makeKeyboardToolBar(title: String) {
        let keyboardToolBar = UIToolbar()
        keyboardToolBar.sizeToFit()

        let flexibleSpace = UIBarButtonItem(barButtonSystemItem:
            UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)

        let bimage = UIImageView(image: UIImage(named: "icon_plus_50"))

        let doneButton = UIBarButtonItem(title: title, style: UIBarButtonItem.Style.done,  target: self, action: #selector(self.doneClicked))

        doneButton.customView = bimage

        keyboardToolBar.setItems([flexibleSpace, doneButton], animated: true)

        self.inputAccessoryView = keyboardToolBar
    }

    @objc func doneClicked() {
        self.endEditing(true)
    }
}

The image appears, but doesn't react. Do not set a custom view works instead, the "title" appears and doneClicked response as appropriate. There are similar questions but unfort. objective-c... Any help appreciate.

Upvotes: 0

Views: 62

Answers (1)

rmaddy
rmaddy

Reputation: 318924

Don't create or use the UIImageView. Just create the UIBarButtonItem with the image.

let doneButton = UIBarButtonItem(image: UIImage(named: "icon_plus_50"), style: .plain, target: self, action: #selector(doneClicked))

No need to set the customView.

Upvotes: 1

Related Questions