Search code examples
uisearchbaruisearchcontroller

Why cannot I subclass UISearchController or UISearchBar


Here is my problem,

I have a custom UIView and added the searchController's searchbar inside that view, but I am having problems (unwanted UI behavior) with the searchController's searchbar in that view, so I created a seperate searchBar, but it does not search. (But does not give me UI problems.)

In the documentation, I saw that the correct way to do this is subclassing UISearchController. Here is the biggest problem, I have no subclassing option for those two. Why would this happen? I already tried cleaning the project, restarting, etc.

No Subclass Option


Solution

  • This seems like a bug in Xcode. Nonetheless, you can create a subclass from these classes manually. Just create an empty Swift file and enter the following:

    import UIKit    
    
    class CustomSearchController: UISearchController {
    
    }
    

    or

    import UIKit    
    
    class CustomSearchBar: UISearchBar {
    
    }