Karu
Karu

Reputation: 1005

How to access searchtextfield of a search bar in IOS 12

I have a code to add done button on top of the keyboard that gets displayed when my search bar is clicked.

let toolbar:UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0,  width: self.view.frame.size.width, height: 30))
        let flexSpace = UIBarButtonItem(barButtonSystemItem:    .flexibleSpace, target: nil, action: nil)
        let doneBtn: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: Selector("doneButtonAction"))
        toolbar.setItems([flexSpace, doneBtn], animated: false)
        toolbar.sizeToFit()

        searchController.searchBar.searchTextField.inputAccessoryView = toolbar

However, the above line works only for IOS 13 and above. In IOS 12, I get an error about

'NSInvalidArgumentException', reason: '-[example.NoCancelButtonSearchBar searchTextField]: unrecognized selector sent to instance

What is the other way to access searchTextField of a searchbar in IOS 12 and below?

Upvotes: 0

Views: 396

Answers (1)

CoachThys
CoachThys

Reputation: 280

The searchTextField is not available before iOS 13.0. What's the purpose of accessing it ?

Is it working if you add your inputAccessoryView one level higher ?

searchController.searchBar.inputAccessoryView = yourAccessoryView

Upvotes: 1

Related Questions