ios 13 - Custom SearchBar with UISearchBar _searchField not working

Pratik Sodha picture Pratik Sodha · Jun 19, 2019 · Viewed 23k times · Source

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.

Answer

Jordan H picture Jordan H · Jun 21, 2019

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

searchBar.searchTextField.backgroundColor = [UIColor blueColor];