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.
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"
}
I added UISearchController programmatically with code
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Введіть значення для пошуку"
searchController.searchBar.backgroundColor = .white
navigationItem.searchController = searchController
definesPresentationContext = true
}
But it seems that it is not centered vertically. How can I fix it?
Connected SearchController to TableView instead of NavigationItem
tableView.tableHeaderView = searchController.searchBar
It seems like a bug in iOS. For some reason at least on iOS 15 searchbar textfield frame has initial value of y = 1 but after you click on the field and then leave this field it will go to y = 8. Fixing the initial frame manually in viewDidLayoutSubviews solved this problem in my case:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
searchController.searchBar.searchTextField.frame.origin.y = 8
}
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 have researched this problem a lot but I have found no solution that would work for me.
Basically, I have a UIViewController that presents UISearchController like this:
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
view.addSubview(searchController.searchBar)
User is then expected to tap the UISearchBar to present searchController and reveal the keyboard. However, a strange thing happens during the transition between controllers.
It seems as if the UISearchController didn't cover the status bar and let you see the UIViewController that presented it below. I would like to find a way to prevent this, i.e. to force the search controller to extend all the way under the status bar.
Things that I have already done:
I have set self.definesPresentationContext = true in viewDidLoad:.
I have found out that this is a known bug, namely rdar://20942583.
I have attempted to circumvent the bug by setting:
self.edgesForExtendedLayout = .All
self.extendedLayoutIncludesOpaqueBars = true
It didn't work.
I'm running out of ideas. Please help.
Thanks a bunch,
Pete.
Facing the same issue and tried everything from here and here and none of this worked for me :(
Best workaround that is working (ugly I know) until I find a better solution:
override func viewDidLoad() {
super.viewDidLoad()
searchController.delegate = self
}
func willPresentSearchController(searchController: UISearchController) {
let statusHeight = UIApplication.sharedApplication().statusBarFrame.size.height
if bgBar == nil {
bgBar = UIView(frame: CGRectMake(0, 0, view.frame.width, (navigationController?.navigationBar.frame.height)! + statusHeight))
bgBar.backgroundColor = UIColor.redColor()
view.addSubview(bgBar)
} else {
bgBar.hidden = false
}
tableView.contentInset.top = statusHeight
}
func willDismissSearchController(searchController: UISearchController) {
bgBar.hidden = true
tableView.contentInset.top = 0
}
On swift 4:
viewWillAppear(_ animated: Bool) {
let statusHeight = UIApplication.shared.statusBarFrame.size.height
let sbview = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: statusHeight))
sbview.backgroundColor = .white
view.addSubview(sbview)
{
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