Swift 3, I'm trying to change the search bars text color from blue to black. For example the "cancel" and the scope bars blue text and border color is blue, I want it black.
This is what I have.
And I tried this line but I don't know enough and this line doesnt work as you can see.
searchController.searchBar.setScopeBarButtonTitleTextAttributes([NSBackgroundColorAttributeName: UIColor.black], for: UIControlState.normal)
viewDidLoad
// Search Bar
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
myTableView.tableHeaderView = searchController.searchBar
// Search Bar Border
let searchBar = searchController.searchBar
searchBar.backgroundImage = UIImage()
// Scope Bar
searchController.searchBar.scopeButtonTitles = ["All", "Released", "Unreleased", "Open Beta"]
searchController.searchBar.delegate = self
rest of searchBar code
// SEARCH BAR: Filtering Content
func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredFollowedArray = followedArray.filter { Blog in
let categoryMatch = (scope == "All") || (Blog.blogType == scope)
return categoryMatch && (Blog.blogName.lowercased().contains(searchText.lowercased()))
}
filteredBlogArray = blogArray.filter { Blog in
let categoryMatch = (scope == "All") || (Blog.blogType == scope)
return categoryMatch && (Blog.blogName.lowercased().contains(searchText.lowercased()))
}
myTableView.reloadData()
}
// SEARCH BAR: Updating Results
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {}
// SEARCH BAR: Scope
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterContentForSearchText(searchText: searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
}
// SEARCH BAR: Updating Scope
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchBar = searchController.searchBar
let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
filterContentForSearchText(searchText: searchController.searchBar.text!, scope: scope)
}
// Deallocating Search Bar
deinit{
if let superView = searchController.view.superview {
superView.removeFromSuperview()
}
}
new code:
// Search Bar
searchController.searchBar.backgroundColor = UIColor.white
searchController.searchBar.barTintColor = UIColor.white
// Coloring SearchBar Cancel button
let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: UIColor.black]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], for: UIControlState.normal)
// Scope: Selected text
let titleTextAttributesSelected = [NSForegroundColorAttributeName: UIColor.white]
UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributesSelected, for: .selected)
// Scope: Normal text
let titleTextAttributesNormal = [NSForegroundColorAttributeName: UIColor.black]
UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributesNormal, for: .normal)
But the scope bar is still blue and it needs to be black
You´re missing a few things, here is the way to do it for your search bar:
let cancelButtonAttributes: NSDictionary =[NSForegroundColorAttributeName: UIColor.black]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], for: UIControlState.normal)
And for your segmented control:
// Selected text
let titleTextAttributesSelected = [NSForegroundColorAttributeName: UIColor.green]
UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributesSelected, for: .selected)
// Normal text
let titleTextAttributesNormal = [NSForegroundColorAttributeName: UIColor.black]
UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributesNormal, for: .normal)