iOS 11 UISearchBar in UINavigationBar

This should work for you

func addSearchbar(){
        if #available(iOS 11.0, *) {
            let sc = UISearchController(searchResultsController: nil)
            let scb = sc.searchBar
            scb.tintColor = UIColor.white

            if let navigationbar = self.navigationController?.navigationBar {
                //navigationbar.tintColor = UIColor.green
                //navigationbar.backgroundColor = UIColor.yellow
                navigationbar.barTintColor = UIColor.blue
            }

            navigationController?.navigationBar.tintColor = UIColor.green
            navigationItem.searchController = sc
            navigationItem.hidesSearchBarWhenScrolling = false
        }
}


Result:

enter image description here

enter image description here


I changed the background of the text field with this code inside AppDelegate.

Swift 4

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        //background color of text field
         UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .cyan
        
        }

This is the result

example used to change the background to cyan color


Now it's what you want...

if #available(iOS 11.0, *) {
            let sc = UISearchController(searchResultsController: nil)
            sc.delegate = self
            let scb = sc.searchBar
            scb.tintColor = UIColor.white
            scb.barTintColor = UIColor.white


            if let textfield = scb.value(forKey: "searchField") as? UITextField {
                textfield.textColor = UIColor.blue
                if let backgroundview = textfield.subviews.first {

                    // Background color
                    backgroundview.backgroundColor = UIColor.white

                    // Rounded corner
                    backgroundview.layer.cornerRadius = 10;
                    backgroundview.clipsToBounds = true;

                }
            }

            if let navigationbar = self.navigationController?.navigationBar {
                navigationbar.barTintColor = UIColor.blue
            }
            navigationItem.searchController = sc
            navigationItem.hidesSearchBarWhenScrolling = false

}

Result:

enter image description here

enter image description here


With Rounded corner:
Animation with rounded corner is also working fine.

enter image description here