iOS 11 UISearchBar in UINavigationBar

試著忘記壹切 提交于 2019-11-27 18:12:02
Krunal

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:


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

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:

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

Objective C

if (@available(iOS 11.0, *)) {
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.delegate = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.navigationItem.searchController=self.searchController;
    self.navigationItem.hidesSearchBarWhenScrolling=NO;
    self.searchController.searchBar.searchBarStyle = UISearchBarStyleProminent;
    self.searchController.searchBar.showsBookmarkButton = NO;
    self.searchController.searchBar.placeholder = @"Search";
    self.searchController.searchBar.tintColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    self.searchController.searchBar.barTintColor=[UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    UITextField *txfSearchField = [self.searchController.searchBar valueForKey:@"_searchField"];
    txfSearchField.tintColor=[UIColor colorWithRed:21/255.0 green:157/255.0 blue:130/255.0 alpha:1];
    txfSearchField.textColor=[UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    txfSearchField.backgroundColor=[UIColor whiteColor];
    UIView *backgroundview= [[txfSearchField subviews]firstObject ];
    backgroundview.backgroundColor=[UIColor whiteColor];
    // Rounded corner
    backgroundview.layer.cornerRadius = 8;
    backgroundview.clipsToBounds = true;
}

This is the code I used to make the search bar white:

if let textfield = searchController.searchBar.value(forKey: "searchField") as? UITextField {
            if let backgroundview = textfield.subviews.first {
                backgroundview.backgroundColor = UIColor.init(white: 1, alpha: 1)
                backgroundview.layer.cornerRadius = 10
                backgroundview.clipsToBounds = true
            }
        }

Try this code,

UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
txfSearchField.backgroundColor = [UIColor redColor];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!