45

Before Xcode-11-Beta (ios13) below code for custom searchbar value for key to get textField working fine. Now getting below crash log.

'NSGenericException', reason: 'Access to UISearchBar's _searchField ivar is prohibited. This is an application bug'

- (UITextField *)textField
{
 return [self valueForKey:@"_searchField"];
}

Any help appreciated.

4
  • 1
    Never use valueForKey: to dig into the private subview structure of standard components. It was never valid and it always ends up breaking after a while. Work with the provide public API to do what you need.
    – rmaddy
    Jun 19, 2019 at 15:19
  • 2
    @rmaddy Thanks for valuable feedback. But for customize UISearchBar already did it. So, now what's solutions. Jun 20, 2019 at 4:06
  • How i am understanding we don't have no one solution?
    – Genevios
    Sep 24, 2019 at 6:33
  • @PratikSodha - check this UISearchBarSearchField BackgroundView color Oct 1, 2019 at 7:26

12 Answers 12

74

The SDK now provides UISearchBar.searchTextField so you can simply replace your private API implementation with the public API.

searchBar.searchTextField.backgroundColor = [UIColor blueColor];
4
  • but now the code doesn't compile on the previous xcode... any suggestions?
    – tomeron11
    Jul 1, 2019 at 8:47
  • And if you need to support iOS 12 and below but you're compiling with Xcode 11?
    – trusk
    Sep 19, 2019 at 12:35
  • 14
    Wrap it in if #available(iOS 13.0, *) {} if you need to support earlier iOS versions. You won’t be able to use an older version of Xcode when building for iOS 13.
    – Jordan H
    Sep 19, 2019 at 14:48
  • 1
    @JordanH you answer is ok , but i need to support xCode10 also anyone can help me
    – guru
    Nov 12, 2019 at 12:34
24

If we want to support iOS 12 and earlier also while compiling with Xcode 11, then we can make an extension of UISearchBar where we can grab the textfield

extension UISearchBar {

    var textField : UITextField? {
        if #available(iOS 13.0, *) {
            return self.searchTextField
        } else { // Fallback on earlier versions
            for subview in subviews.first?.subviews ?? [] {
                if let textField = subview as? UITextField {
                    return textField
                }
            }
        }
        return nil
    }
}

USAGE

searchBar.textField?.font = UIFont.systemFont(ofSize: 15.0)

Here we can access all textField's properties and modify as per our needs in UISearchBar

There is a change in the view hierarchy. So by printing self.subviews[0]).subviews where self in UISearchBar


For iOS 12 and earlier

enter image description here

For iOS 13+

enter image description here

Along with UISearchBarBackground, now we have UISearchBarSearchContainerView and UISearchBarScopeContainerView whereas UISearchBarTextField is missing which is replaced by an extension provided by Apple in UISearchTextField class

extension UISearchBar {

    
    open var searchTextField: UISearchTextField { get }
}

So we can directly access searchBar.searchTextField in iOS 13 and above devices whereas it will lead to a crash if we try to access this property in iOS 12 and below OS devices.

The crash will be like this:

[UISearchBar searchTextField]: unrecognized selector sent to instance

enter image description here

Here is the Apple doc for this

4
  • 4
    Why does Xcode compiler not throw a warning when the iOS deployment target is set to < 13.0? Doesn't that happen usually when using an API that is not available on the deployment target version? And isn't that an essential job of a pre-compiler? 🙄
    – Manuel
    Oct 15, 2019 at 15:32
  • Yes, and UISearchTextField IS marked in (e.g. typing "UISearchTextField * textfield" does give me a warning), but unfortunately, the property "searchBar.searchTextField" isn't marked as iOS13 only in the SDK.
    – mackworth
    Dec 6, 2019 at 4:37
  • Crash on iOS 12 when I call becomeFirstResponder.
    – BSK-Team
    Jan 7, 2020 at 15:12
  • 1
    Last time I checked (on Xcode 11.6) this property finally flagged as iOS 13+. It was about time...
    – gcharita
    Aug 11, 2020 at 16:19
22

I had the same crash, with IOS 13 you don't need anymore the .value(forKey:)

Here is the line that caused the crash:

if let searchField = searchController.searchBar.value(forKey: "_searchField") as? UITextField {

And this was the fix:

let searchField = searchController.searchBar.searchTextField
3
  • 1
    How did you find the line that caused the crash?
    – Marlhex
    Sep 21, 2019 at 22:16
  • It worked but my question is earlier value for key method used to work normally, now on xcode 11.2.1 what it causing it to crash? Nov 19, 2019 at 11:45
  • This helped me after days of nightmares
    – faris97
    May 5, 2020 at 16:15
9

Do this:

 var searchTextField: UITextField?
    if #available(iOS 13.0, *) {
        searchTextField = searchBar.searchTextField
    } else {
        if let searchField = searchBar.value(forKey: "searchField") as? UITextField {
            searchTextField = searchField
        }
    }

This will give you search bar's text field appropriately based on version check.

7

It still works for me with key "searchField" without underscore. Swift 5

guard let searchField = searchBar.value(forKey: "searchField") as? UITextField else { return }
2
  • You have to do it this way if you are still using Xcode 10 since there is no "searchTextField" of SDK 10 UISearchBar. But when running under iOS 13 if will work. ` if #available(iOS 13.0, *) { return value(forKey: "searchField") as? UITextField } else { return value(forKey: "_searchField") as? UITextField ` Oct 4, 2019 at 1:15
  • is it a private API? will Apple reject the app to app store?
    – HamasN
    Feb 26, 2021 at 7:50
4

If u want check iOS Version!

 if #available(iOS 13.0, *) {
    searchBar.searchTextField.clearButtonMode = .never
 } else {
    searchBar.textField.clearButtonMode = .never
 }

Dont Forget the extension:

extension UISearchBar {

 var textField : UITextField{
    return self.value(forKey: "_searchField") as! UITextField
 }
}
1
  • Day saving answer! Hint: For me, it just worked without the underscore (self.value(forKey: "searchField") as! UITextField) Nov 1, 2019 at 10:07
3

Removed the "_" Underscore (while trying to access property) everywhere and it worked for all iOS versions.

2
extension UISearchBar {

    var textField : UITextField? {
        if #available(iOS 13.0, *) {
            return self.searchTextField
        } else {
            // Fallback on earlier versions
            return value(forKey: "_searchField") as? UITextField
        }
        return nil
    }
}
0

You can make check like this for iOS 11 and the earlier versions:

if #available(iOS 11, *){
            self.searchBar.placeholder = "Search by Name"
            self.searchBar.setSerchFont(textFont: UIFont(name:AppFontLato.Regular, size:14.0 * DeviceInfo.aspectRatio()))
            self.searchBar.setSerchTextcolor(color: Colors.black51)
        }
        else{
            if let searchTextField = self.searchBar.value(forKey: "_searchField") as? UITextField, let clearButton = searchTextField.value(forKey: "_clearButton") as? UIButton {
                searchTextField.placeholder = "Search by Name"
                searchTextField.font = UIFont(name:AppFontLato.Regular, size:14.0 * DeviceInfo.aspectRatio())
                searchTextField.textColor = Colors.black51
            }
        }

Hope this helps

0

I'm getting crash on cancel button value forkey. Is it possible to customize cancel button in iOS 13.enter image description here

4
  • How are you accessing the cancel button?
    – mohsin
    Nov 13, 2019 at 17:15
  • let cancelButton = searchBar.value(forKey: "_cancelButton")as? UIButton @mohsin
    – Jiffin
    Nov 14, 2019 at 5:11
  • 1
    Fixed the issue by removing the underscore before the key. Swift let cancelButton = searchBar.value(forKey: "cancelButton")as? UIButton
    – Jiffin
    Nov 16, 2019 at 11:10
  • Yes, remove "_" from wherever you are trying to access. It will work for almost all versions
    – mohsin
    Nov 18, 2019 at 5:58
0

You can use this answer, working fine in iOS 13:-

https://stackoverflow.com/a/60503506/9863222

0
if #available(iOS 13.0, *) {
        self.searchBar.searchTextField.clearButtonMode = .never
    } else {
        // Fallback on earlier versions
        if let searchField = searchBar.value(forKey: "searchField") as? UITextField {
            searchField.clearButtonMode = .never
        }
    }

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.