iOS 11 UISearchBar in UINavigationBar

后端 未结 7 1003
有刺的猬
有刺的猬 2020-12-04 17:15

I want to place a search bar in the new navigation bar with the new iOS 11 large titles. However, the color of the search bar is automatically applied by iOS and I can\'t ch

相关标签:
7条回答
  • 2020-12-04 17:37

    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.

    0 讨论(0)
提交回复
热议问题