Use Background Image on Uisearchcontroller iOS 11

Use background image on UISearchController iOS 11

Thanks to Krunal's answer I was able to find a decent solution after searching for quite some time.

This is how I came about implementing the background-image for iOS 11:

    // Implement Search Controller
if #available(iOS 11.0, *) {
if let navigationBar = self.navigationController?.navigationBar {
navigationBar.barTintColor = UIColor(patternImage: gradientImage!)
}
if let textField = searchController.searchBar.value(forKey: "searchField") as? UITextField {
if let backgroundview = textField.subviews.first {
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;

}
}
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
} else {
tableView.tableHeaderView = searchController.searchBar
}

Change UISearchBar background image in iOS11

You may be looking for this:

if #available(iOS 11.0, *) {
let sc = UISearchController(searchResultsController: nil)
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor(patternImage: UIImage(named: "pattern")!)
}
navigationItem.searchController = sc
navigationItem.hidesSearchBarWhenScrolling = false
}

Here is image:

Sample Image

And here is result:

Sample Image

Here is more detailed answer, if you want to change any other UI elements of search bar.

  • iOS 11 UISearchBar in UINavigationBar

How to change UISearchController SearchBar Colour in iOS 11

I have put the code in the main queue and its work.

DispatchQueue.main.async {
self.searchBarController.searchBar.placeholder = "Search"
self.searchBarController.searchBar.setTextColor(color: .white)
self.searchBarController.searchBar.setPlaceholderTextColor(color: .white)
self.searchBarController.searchBar.setSearchImageColor(color: .white)
self.searchBarController.searchBar.setTextFieldColor(color: UIColor.clear) //light green
self.searchBarController.searchBar.delegate = self
}

Strange :)

How to change background color of the text field in the UISearchController?

Here is a an example on how to set the textField background.

class ViewController: UIViewController {

let searchController = UISearchController(searchResultsController: nil)

private lazy var searchTextField: UITextField? = { [unowned self] in
var textField: UITextField?
self.searchController.searchBar.subviews.forEach({ view in
view.subviews.forEach({ view in
if let view = view as? UITextField {
textField = view
}
})
})
return textField
}()

override func viewDidLoad() {
super.viewDidLoad()

searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Candies"
navigationItem.searchController = searchController
definesPresentationContext = true

if let bg = self.searchTextField?.subviews.first {
bg.backgroundColor = .green
bg.layer.cornerRadius = 10
bg.clipsToBounds = true
}
}
}

Result

Sample Image



Related Topics



Leave a reply



Submit