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
updated picture
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)
Related
I am getting idea of a new app. I love to find a many tricks. however, No luck for me.
The simple.
You can see swipe down the search bar from the below of the navigation bar.
Result:
// iOS 13 Navigation Bar only
self.navigationItem.title = "Search Title"
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationItem.largeTitleDisplayMode = .never
UINavigationBar.appearance().isTranslucent = false
let app = UINavigationBarAppearance()
let navigationBar = self.navigationController?.navigationBar
app.backgroundColor = .clear
app.configureWithOpaqueBackground()
app.titleTextAttributes = [.foregroundColor: UIColor.label]
app.largeTitleTextAttributes = [.foregroundColor: UIColor.label]
app.backgroundColor = .systemGroupedBackground
self.navigationController?.navigationBar.scrollEdgeAppearance = app
navigationBar!.standardAppearance = app
navigationBar!.scrollEdgeAppearance = app
Search Result:
// Search Bars
let search = UISearchController(searchResultsController: nil)
search.searchBar.delegate = self
search.searchResultsUpdater = self as? UISearchResultsUpdating
search.searchBar.placeholder = "Search"
search.searchBar.searchTextField.tintColor = UIColor.gray
search.searchBar.setImage(UIImage(named: "magnifyingglass")?.withTintColor(UIColor.systemGray), for: .search, state: .normal)
self.navigationItem.searchController = search
(UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]) ).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.init(white: 100, alpha: 0.50)]
let textField = search.searchBar.value(forKey: "searchField") as! UITextField
let glassIconView = textField.leftView as! UIImageView
glassIconView.image = glassIconView.image?.withRenderingMode(.alwaysTemplate)
glassIconView.tintColor = UIColor.systemGray
let clearButton = textField.value(forKey: "clearButton") as! UIButton
clearButton.setImage(clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate), for: .normal)
clearButton.tintColor = UIColor.systemGray
Now, I am tried to look for the trick code to set the search bar's text will appear navigation bar when you have searched it. (Yes, It is a very familiar to Safari style).
I don't like Search Bar title text stayed on the small of the search bars, so move to a large title look better.
Let me know. :)
You can use searchBarSearchButtonClicked method of UISearchBarDelegate.
(https://developer.apple.com/documentation/uikit/uisearchbardelegate/1624294-searchbarsearchbuttonclicked)
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.navigationItem.title = searchBar.text ?? "Search Title"
}
If you need to update the title as the user types in search bar, use updateSearchResults of UISearchResultsUpdating. (https://developer.apple.com/documentation/uikit/uisearchresultsupdating/1618658-updatesearchresults)
func updateSearchResults(for searchController: UISearchController) {
self.navigationItem.title = searchController.searchBar.text ?? "Search Title"
}
See screenshot here
How do I change the color of the background (not search bar textfield)
Also how do I increase the gap between status bar and search bar? It looks too close.
I have tried this but doesn't work as expected:
func configureSearchController ()
{
searchController = UISearchController(searchResultsController: resultsController)
searchController.searchResultsUpdater = self
searchController.searchBar.layer.borderWidth = 0;
//searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.barTintColor = UIColor.searchBarBackgroundGrey()
for subView in searchController.searchBar.subviews {
for subViewOne in subView.subviews {
if subViewOne is UITextField {
subViewOne.backgroundColor = UIColor.searchBarTextFieldGrey()
break
}
}
}
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.barTintColor = UIColor.white
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.barTintColor = UIColor.searchBarBackgroundGrey()
}
You need to set the delegate of the search bar in the viewDidLoad() method:
searchController.searchBar.delegate = self
Im adding refresh control to tableView.
then start animate it from code dosent show on top. I must scroll tableView to down.
And after this i add Search controll to navbaritem then its look like:
here is code
lazy var customRefreshControl: UIRefreshControl = {
let control = UIRefreshControl()
control.attributedTitle = NSAttributedString(string: "Downloading Locations")
control.addTarget(self, action: #selector(updateLocation), for: .valueChanged)
return control
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.largeTitleDisplayMode = .automatic
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.tintColor = .white
searchController.searchBar.delegate = self
tableView.addSubview(customRefreshControl)
title = "location_groups".localized()
tableView.tableFooterView = UIView()
tableView.backgroundColor = .groupTableViewBackground
navigationItem.searchController = searchController
searchController.dimsBackgroundDuringPresentation = false
}
func loadLocationGroups() {
DispatchQueue.global(qos: .userInteractive).async { [unowned self] in
self.locationGroups = DataBaseManager.Instance.GetLocations()
DispatchQueue.main.async {
self.tableView.reloadData()
if self.locationGroups.isEmpty {
self.customRefreshControl.attributedTitle = NSAttributedString(string: "Downloading Locations")
} else {
self.customRefreshControl.attributedTitle = NSAttributedString(string: "Synchronize")
}
self.customRefreshControl.beginRefreshing()
}
self.request(requestType: .getLocationGroups)
}
}
Using Swift 3 and testing on my device, I've tried several code that should remove the black/grey border but it's not being removed. The weird thing is the border is there but once I click on the searchBar ready to type, the border isn't there anymore until the view is loaded again. So the border is just showing until I click the searchBar.
This is my code:
// Coloring TableView
myTableView.backgroundColor = UIColor.white
myTableView.sectionIndexBackgroundColor = UIColor.black
myTableView.sectionIndexColor = UIColor.black
// 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)
// Coloring Scope Bar
UISegmentedControl.appearance().tintColor = UIColor.black
UISegmentedControl.appearance().backgroundColor = UIColor.white
// Search Bar
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
myTableView.tableHeaderView = searchController.searchBar
// Scope Bar
searchController.searchBar.scopeButtonTitles = ["All", "Released", "Unreleased", "Open Beta"]
searchController.searchBar.delegate = self
let searchController = UISearchController(searchResultsController: nil)
// 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()
}
}
This is how the searchBar looks when the view is first loaded, the line on the top is grey (I don't why here the grey looks so faded, you can't see it but a very thin line of grey is there):
and after you click the searchBar, the line turns black:
And this happens when I add this code which was referred to me:
// Search Bar Border
let searchBar = searchController.searchBar
searchBar.backgroundImage = UIImage()
and this is after I click on the searchBar with that code, and how it's supposed to look. No border:
Are you checking this behavior in the small sized simulator only? The black line in your second screen shot doesn't look connected to your search bar in the first screenshot. It looks like the bottom of the status bar instead. Sometimes simulator can't display thin lines properly. I would increase the simulator size to the biggest or better yet, test it on a phone.
I would like uisearchcontroller to start searching after I type at least three characters in search bar. So, what should I do for that ?
func configureSearchController() {
// Initialize and perform a minimum configuration to the search controller.
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
let textFieldInsideSearchBar = searchController.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.font = UIFont(name: "Bauhaus", size: 19)
searchController.searchBar.setImage(UIImage(named: "searchikon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal);
// Place the search bar view to the tableview headerview.
TableView.tableHeaderView = searchController.searchBar
All you need to is add the single required method for the UISearchController.
func updateSearchResultsForSearchController(searchController: UISearchController) {
if searchController.searchBar.text?.characters.count > 2 {
// Filter your search results here
}
}
You will want to check the the length of the characters in an event which checks the change in the text field:
nameOfString.characters.count