iosswiftuikituisearchbaruiappearance

How to Change Keyboard Return key type in UISearchBar


I would like to change the string title of the return key of a keyboard invoked by a searchBar in my iOS App. I want to change the title from "Search" to "Done".

My searchBar is programmatically created inside a UIView.

I have tried to place the following snippet in either the viewDidLoad() of the UIView or in the searchBar setup method (see below error report).

DispatchQueue.main.async {
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).returnKeyType = UIReturnKeyType.done
}

It fails to change the title of the Return key in either place. Further, it oddly flags an error that I tried setting the return type outside the MainThread when clearly that is not the case. Here is the error:

2023-06-28 08:15:06.873302-0400 MyApp[28389:4455820] 
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'Have you sent -setReturnKeyType: to 
    <UISearchBarTextField: 0x7fccbc856a00; 
    frame = (0 0; 0 0); opaque = NO; 
    gestureRecognizers = <NSArray: 0x600003807cc0>; 
    borderStyle = RoundedRect; 
    background = <_UITextFieldSystemBackgroundProvider: 
      0x60000355eb40: 
       backgroundView=<_UISearchBarSearchFieldBackgroundView: 
         0x7fccbc75d020; 
       frame = (0 0; 0 0); 
       opaque = NO; autoresize = W+H; 
       userInteractionEnabled = NO; 
       backgroundColor = <UIDynamicSystemColor: 0x60000218fd00; 
       name = tertiarySystemFillColor>; 
       layer = <CALayer: 0x60000355ef00>>, 
    fillColor=(null), 
    textfield=<UISearchBarTextField 0x7fccbc856a00>>; 
  layer = <CALayer: 0x60000355fa80>> 

off the main thread? 

To verify, look for a complaint in the logs: "Unsupported use of UIKit…", and fix the problem if you find it. If your use is main-thread only please file a radar on UIKit, and attach this log. exercisedImplementations = { "setReturnKeyType:" = ( );}

Any thoughts? As we all know, a Radar takes weeks to months, if ever.

SearchBar setup method:

func setupSearchBar() -> Void {
    myBar = UISearchBar()
    myBar.searchBarStyle = .prominent
    myBar.sizeToFit()
    myBar.isTranslucent = false
    myBar.delegate = self
    self.addSubview(myBar)
    myBar.translatesAutoresizingMaskIntoConstraints = false
    let barLeft = myBar.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0)
    let barRight = myBar.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0)
    let barTop = myBar.topAnchor.constraint(equalTo: topAnchor, constant: 0)
    let barHeight = myBar.heightAnchor.constraint(equalToConstant: 40.0)
    let barWidth = myBar.widthAnchor.constraint(greaterThanOrEqualToConstant: 220.0)
    barRight.priority = UILayoutPriority(998.0)
    barLeft.isActive = true
    barRight.isActive = true
    barTop.isActive = true
    barHeight.isActive = true
    barWidth.isActive = true
    DispatchQueue.main.async {
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).returnKeyType = UIReturnKeyType.done
    }
}

Solution

  • try to add that line:

    myBar.returnKeyType = .done