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"
}
Related
I am trying to make searchBar corner rounded. I have tried many ways of using layer.cornerRadius, maskToBound = true. I am loading the searchBar in the navigationBar programmatically. I have tried many solution given on stack-overflow but nothing worked.
func setupsearchbar()
{
// Setup the Search Controller
searchcontroller.searchResultsUpdater = self as? UISearchResultsUpdating
searchcontroller.hidesNavigationBarDuringPresentation = false
searchcontroller.searchBar.placeholder = "Search"
self.navigationItem.titleView = searchcontroller.searchBar
self.searchcontroller.searchBar.layer.cornerRadius = 30
self.searchcontroller.searchBar.layer.masksToBounds = true
definesPresentationContext = true
searchcontroller.searchBar.delegate = self
}
Please help if there is anything I can do.
Use following code which are working fine for me in my current project.
if let textfield = searchController.searchBar.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;
}
}
I hope this will help you.
In viewDidLoad():
searchController = UISearchController(searchResultsController: nil)
let cancelButtonAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)
searchController?.dimsBackgroundDuringPresentation = false
searchController?.searchResultsUpdater = self
searchController?.delegate = self
searchController?.searchBar.tintColor = UIColor.black
searchController?.searchBar.barTintColor = UIColor(white: 0.9, alpha: 0.9)
searchController?.searchBar.placeholder = NSLocalizedString("Search", comment: "")
searchController?.hidesNavigationBarDuringPresentation = false
self.navigationItem.titleView = self.searchController?.searchBar
searchController?.isActive = true
self.searchController?.becomeFirstResponder()
self.searchController?.searchBar.becomeFirstResponder()
Here is the delegate method:
extension SearchMembers : UISearchControllerDelegate {
func didPresentSearchController(searchController: UISearchController){
self.searchController?.searchBar.becomeFirstResponder()
}
}
In my test didPresentSearchController(searchController:) is never called.
I don't know why but whatever I do just doesn't work.
Any idea?
EDIT
The only solution that works
delay(0.6) {
self.searchController?.searchBar.becomeFirstResponder()
}
Which creates an unwanted lag
According to the documentation of didPresentSearchController:
This method is only called when the search controller is automatically presented. It is not called if you explicitly present the search controller.
If you want to present the search results interface explicitly, wrap your search controller in a UISearchContainerViewController object and present that object instead.
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'm having a weird problem, when my app first loads you cannot enter text in the search bar no matter how many times you tap it, the search bar is nested in the navigation bar.
My app also use a tab bar, and when you switch tabs then go back to the tab with the search bar it allows you enter text... any ideas what's causing this?
Heres the code for the searchBar:
func setupSearchBar(){
let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTable") as! LocationSearchTableViewController
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController?.searchResultsUpdater = locationSearchTable
searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Location"
searchBar.isTranslucent = true
searchBar.isUserInteractionEnabled = true
for subView in searchBar.subviews{
for subsubView in subView.subviews{
if let textField = subsubView as? UITextField{
var currentTextFieldBounds = textField.bounds
currentTextFieldBounds.size.height = 40.0
textField.bounds = currentTextFieldBounds
textField.borderStyle = UITextBorderStyle.none
textField.textAlignment = NSTextAlignment.left
textField.font = UIFont(name: "System", size: 25.0)
textField.textColor = theme?.textColour
}
}
}
self.navigationController?.navigationBar.setBarColour(colour: (theme?.tabBarColour)!, tint: (theme?.textColour)!)
navigationItem.titleView = resultSearchController?.searchBar
navigationItem.titleView?.bringSubview(toFront: (resultSearchController?.searchBar)!)
searchBar.delegate = self
searchBar.showsSearchResultsButton = true
searchBar.setImage(#imageLiteral(resourceName: "location_icon.png"), for: UISearchBarIcon.resultsList, state: UIControlState.normal)
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
locationSearchTable.mapView = mapView
locationSearchTable.handleMapSearchDelegate = self
}
Ok after a lot messing around, I discovered that in my custom UITabBarController I had used override func viewWillAppear(_ animated: Bool) without adding super.viewWillAppear() and that caused the problem! I assume because of that subviews weren't being laid out correctly. Hope that helps anyone who has a similar problem to mine.
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