UISearchController iOS 11 Customization

后端 未结 9 1194
南旧
南旧 2021-01-29 21:40

I had been using the following code prior to iOS 11 to customize the appearance of the UISearchController search bar:

var searchController = UISearc         


        
相关标签:
9条回答
  • 2021-01-29 22:20

    You need to find the UISearchBar's underlying UITextField and change its text color.

    Notice this only have effect when search controller is going to present (UISearchControllerDelegate.willPresentSearchController) or presented.

    class ViewController : UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // setup your search controller...
    
            // set search controller's delegate
            navigationItem.searchController?.delegate = self
        }
    }
    
    extension ViewController : UISearchControllerDelegate {
    
        func willPresentSearchController(_ searchController: UISearchController) {
            // update text color
            searchController.searchBar.textField?.textColor = .white
        }
    }
    
    extension UISearchBar {
    
        var textField: UITextField? {
            for subview in subviews.first?.subviews ?? [] {
                if let textField = subview as? UITextField {
                    return textField
                }
            }
            return nil
        }
    }
    
    0 讨论(0)
  • 2021-01-29 22:22

    Try it: searchController.<YOUR SEARCHBAR>.barStyle = .blackOpaque instead of self.searchBarStyle = .minimal.

    Thus:

    var searchController = UISearchController(searchResultsController: nil)
    searchController.searchBar.setDefaultSearchBar()
    //Add this line below
    searchController.searchBar.barStyle = .blackOpaque
    searchController.searchResultsUpdater = self
    
    if #available(iOS 11.0, *) {
        navigationItem.searchController = searchController
    } else {
        tableView.tableHeaderView = searchController.searchBar
    }
    
    extension UISearchBar {
        func setDefaultSearchBar() {
            self.tintColor = UIColor.blue
            //Delete this line below
            self.searchBarStyle = .minimal
            self.backgroundImage = UIImage(color: UIColor.clear)
            let searchBarTextField = self.value(forKey: "searchField") as! UITextField
            searchBarTextField.textColor = UIColor.white
            searchBarTextField.tintColor = UIColor.blue
            searchBarTextField = .dark
        }
    }
    
    0 讨论(0)
  • 2021-01-29 22:28

    I just found out how to set them: (with some help of Brandon and Krunal, thanks!)

    The "Cancel" text:

    searchController.searchBar.tintColor = .white
    

    The search icon:

    searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.search, state: .normal)
    

    The clear icon:

    searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.clear, state: .normal)
    

    The search text:

    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
    

    The placeholder:

    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
    

    The white background:

    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
    }
    

    Taken from here.

    0 讨论(0)
  • 2021-01-29 22:28

    Moving the call to setDefaultSearchBar into viewDidAppear should fix this.

    0 讨论(0)
  • 2021-01-29 22:31
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    
    0 讨论(0)
  • 2021-01-29 22:31

    You have to access the UITextField inside the UISearchBar. You can do that by using

    let textFieldInsideSearchBar = yourSearchbar.value(forKey: "searchField") as? UITextField
    
    textFieldInsideSearchBar?.textColor = yourcolor
    

    OR

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