Searchbar cancel button text is truncated - swift

Having various problems setting up the UISearchBar to work properly. It should be shown in the navigationBar. Using the "old" way setting navigationItem.titleView = searchController.searchBar. As seen on the picture below, the Cancel button text is truncated. Any ideas how to solve this problem? I have tried to set searchController.searchBar.sizeToFit() with no luck.
fileprivate lazy var searchController: UISearchController = {
let sc = UISearchController(searchResultsController: nil)
sc.delegate = self
sc.searchBar.delegate = self
sc.searchBar.returnKeyType = .done
sc.searchBar.tintColor = UIColor.black
sc.searchResultsUpdater = self
sc.hidesNavigationBarDuringPresentation = false
sc.dimsBackgroundDuringPresentation = false
return sc
} ()

Related

Cant change colour of searchBar and it jumps to top of view when selected

I'm trying to attach a search bar to the top of my tableView and change its attributes (eg. colour, placeholder). However, I can't figure out how. I've tried embedding the tableView in another view but that didn't help. Any ideas?
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.tintColor = UIColor.red
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
search.searchBar.placeholder = "Type something here to search"
navigationItem.searchController = search
tableView.tableHeaderView = search.searchBar
}
This function is called in the viewDidLoad() and the tableView is added but not with the right colour or placeholder and jumps to the top of the screen when selected.
Any help would be appreciated.
This is the updated code for setupSearch (everything is working fine except the bar jumps to the top when selected):
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
tableView.tableHeaderView = search.searchBar
}
I declare the search bar at the start using:
let search = UISearchController(searchResultsController: nil)
Any ideas on how to stop the bar jumping to the top?
Just add search bar as tableview's headerview not with navigation's item searchcontroller(not add search bar with both(tableview and navigation) as in your code). You can try with updated code below:
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
search.searchBar.placeholder = "Type something here to search"
tableView.tableHeaderView = search.searchBar
self.definesPresentationContext = true
}
override func viewDidLoad() {
super.viewDidLoad()
setupSearch()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil) // be notified when the keyboard changes your table View frame
}
#objc func keyboardWillShow(notification: NSNotification) {
tableView.frame.origin.y = 0 // reset the table view to its original coordinates
}
func setupSearch(){
// for iOS 12 and lower, you can change the placeholder like this :
let textFieldSearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldSearchBar?.textColor = .red
let searchBarLabel = textFieldSearchBar!.value(forKey: "placeholderLabel") as? UILabel
textFieldSearchBarLabel?.textColor = .red
}

How to adjust constraints with collapsing SearchBar in large iOS11 NavigationBar

How do I adjust the constraint when pushing a new UIViewController when a UISearchController is implemented as self.navigationItem.searchController = search
New controller pushed: SearchBar collapses but leaves View with constrait to top = 0 on the initial position.
(The background of the ViewController is yellow for easy understanding of the matter)
Implementation:
viewDidLoad() {
if #available(iOS 11.0, *) {
let search = UISearchController(searchResultsController: nil)
search.searchResultsUpdater = self
search.definesPresentationContext = true
search.hidesNavigationBarDuringPresentation = false
search.searchBar.tintColor = UI.COLOR_WHITE
search.dimsBackgroundDuringPresentation = false
self.navigationItem.hidesSearchBarWhenScrolling = false
self.navigationItem.searchController = search
}
I have tried using self.automaticallyAdjustsScrollViewInsets = false without success and the tableView implements self.contentInsetAdjustmentBehavior = .always
What am I missing? Help is very appreciated.

UISearchBar alignment issues in iOS 11

I have UITableViewController where i was adding UISearchController searchbar as an headerView and I have changed this to navigationItem.searchcontroller for iOS 11 in iOS 11 searchbar won't appear at all even navigationItem was present.
if don't use this searchbar UI is going for toss. Please suggest me what was i am doing wrong here.
let searchController = UISearchController(searchResultsController: vc)
searchController.searchBar.scopeButtonTitles = ["All", "images", "videos", "others"]
searchController.dimsBackgroundDuringPresentation = false
// Don't remove these two lines then search bar alignment issue will raise
self.extendedLayoutIncludesOpaqueBars = true
self.searchController.searchBar.isHidden = false
definesPresentationContext = false
if #available(iOS 11.0, *) {
self.navigationItem.searchController = searchController // not working for me.
searchController?.hidesNavigationBarDuringPresentation = false
navigationItem.hidesSearchBarWhenScrolling = false
} else {
searchController?.hidesNavigationBarDuringPresentation = true
searchController?.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
}
List of issues are
on iOS 11 with UI goes wrong as attached image if add UISearchbar to header view
if I try add to navigation item as mentioned in the code it is not appearing
As i am using different search results controller when search is active i need to disable actions in present controller - how can i achieve this easily.
I'm not really sure what went wrong there but in my case, it's working fine. Here is my code:
Created a searchController file:
private let searchController = UISearchController(searchResultsController: nil)
In viewDidLoad():
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = kSearchPlaceholder
searchController.searchBar.scopeButtonTitles = [kCategory, kAuthor, kPicture]
searchController.searchBar.delegate = self
definesPresentationContext = true
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
tableView.tableHeaderView = searchController.searchBar
}
And it's good to go.

After updating to iOS11 my searchBar behaves strange

After updating to iOS11 my searchBar behaves very strange. When activated, it jumps to the top of screen. It works as it should, as before but of course I want it to stay in place. I have tried a lot of solutions from googling this behavior but nothing helps. Do anyone have the same problem? And what did you do to solve this?
searchBar in place
searchBar jumped to top
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
// Setup the Search Controller
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.preservesSuperviewLayoutMargins = true
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.sizeToFit()
walkaboutTableView.tableHeaderView = searchController.searchBar
searchController.searchBar.barTintColor = matildaLightBlue
searchController.searchBar.clipsToBounds = true
searchController.searchBar.layer.borderWidth = 2
searchController.searchBar.layer.borderColor = UIColor.black.cgColor
}
Adding search controller instance into navigation item instead of table header view helps me to solve similar issue in my project.
if #available(iOS 11, *) {
navigationItem.searchController = searchController
} else {
tableView.tableHeaderView = searchController.searchBar
}
navigationItem.searchController
navigationItem.hidesSearchBarWhenScrolling
Ruslan Kolosovskyi's answer is really good. In addition if you want the search bar to be more accessible:
if #available(iOS 11, *) {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
} else {
tableView.tableHeaderView = searchController.searchBar
}

Set textfield in UISearchBar

I'm trying to add SearchController to UINavigationBar. I'm trying to set UITextField of UISearchBar after back button of UINavigationController. I want some more space after back button
When I start searching it appears as
Whereas I should be able to view back button. Only textfield width should be decreased. Also after Cancel it should be again back to initial layout. Whereas it is displayed as below:
Below is my code
var searchResultController = UISearchController()
self.searchResultController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.delegate = self
controller.hidesNavigationBarDuringPresentation = false
self.navigationController?.navigationBar.addSubview(controller.searchBar)
return controller
})()
override func viewDidAppear(animated: Bool) {
for subView in searchResultController.searchBar.subviews{
for subsubView in subView.subviews {
if let textField = subsubView as? UITextField {
var bounds: CGRect
bounds = textField.frame
bounds.size.width = self.view.bounds.width - 50
}
}
}
}
Please let me know how can I fix this.
Thanks in advance
For setting the UIsearchBar add it to Navigation's titleView as
self.navigationItem.titleView = controller.searchBar
For removing Cancel button we can use UISearchControllerDelegate method
func didPresentSearchController(searchController: UISearchController) {
searchController.searchBar.showsCancelButton = false
}
Hope this may help any one.