func configureSearchController()
{
resultsController.tableView.delegate = self
resultsController.tableView.dataSource = self
self.searchController = UISearchController(searchResultsController: self.resultsController)
//self.tableView.tableHeaderView = self.searchController.searchBar
self.searchController.searchResultsUpdater = self
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.sizeToFit()
searchController.searchBar.delegate = self
self.searchController.searchBar.scopeButtonTitles = []
for subView in searchController.searchBar.subviews {
for subViewOne in subView.subviews {
if subViewOne is UITextField {
searchTextField = subViewOne as! UITextField
subViewOne.backgroundColor = UIColor.white
var currentTextFieldBounds = subViewOne.bounds
currentTextFieldBounds.size.height = 45
subViewOne.bounds = currentTextFieldBounds
break
}
}
}
extendedLayoutIncludesOpaqueBars = true
definesPresentationContext = true
}
This doesn't change height of text field unexpectedly. I also want to change height of search bar. What changes should I make here for the same to work?
IMO, The search bar size shouldn't be changed as it's the native standard provided by Apple. Also the way you use of recursively searching of textfield is not recommended and not guaranteed to work in all iOS versions.
Maybe you can try to use custom Search bar with your own text field and u can easily play with it.
Related
I'm try to build search function using tutorial from https://www.thorntech.com/2016/01/how-to-search-for-location-using-apples-mapkit/
I need to change his code
because as you can see from the picture if I do navigationitem.titleview the plus button disappears. I want to use searchbar when I searching but I couldn't find a way to do it
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for places"
navigationItem.titleView = resultSearchController?.searchBar
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
locationSearchTable.mapView = mapView
locationSearchTable.handleMapSearchDelegate = self
Instead of
navigationItem.titleView = resultSearchController?.searchBar
Try
navigationItem.searchController = resultSearchController
have a weird situation here. When I use the UISearchController, I get this appearance first (as expected)
But when you select inside the TextField to start searching, the Status Bar becomes completely white (or black if you are in dark mode)
This never used to happen. Is there some setting in UISearchController that tells it to use a certain Status Bar style when using the Search Bar?
I'd prefer it stay the color it was before selecting the TextField
---EDIT---
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItem.Style.plain, target: nil, action: nil)
frc = getFRC()
frc.delegate = self
self.resultsSearchController.delegate = self
let searchBar = self.resultsSearchController.searchBar
self.resultsSearchController.searchResultsUpdater = self
self.resultsSearchController.obscuresBackgroundDuringPresentation = false
self.resultsSearchController.extendedLayoutIncludesOpaqueBars = true
searchBar.sizeToFit()
self.tableView.tableHeaderView = searchBar
searchBar.placeholder = "Catalog Search"
searchBar.barTintColor = UIColor.darkAqua
searchBar.searchTextField.backgroundColor = UIColor.white
self.definesPresentationContext = true
searchBarHeight = searchBar.frame.height
do {
try frc.performFetch()
} catch {
error.tryError(tryMessage: "Perform initial fetch", loc: self)
}
if tutorials.catalog {
createTutorialTab(segueNameOnOpen: "catalogTutorial")
}
}
You use this function to change status bar colors, it's a kind of hack to deal with status bar ;). If you are using one theme status bar in whole app , call this function from didFinishLaunching in AppDelegate.
func changeStatusBar(backgroundColor: UIColor, contentColor:UIColor) {
if let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView {
statusBar.backgroundColor = backgroundColor
statusBar.setValue(contentColor, forKey: "foregroundColor")
}
}
I am including a search bar by code and enabling interaction using the code below, the search bar is displayed but when I click on the field editing is not enabled:
let searchTableViewController = storyboard?.instantiateViewController(withIdentifier: "SearchBarTableVC") as? SearchBarTableVC
searchTableViewController?.handleMapSearchDelegate = self
searchTableViewController?.mapView = self.mapView
searchBarController = UISearchController(searchResultsController: searchTableVC)
searchBarController?.searchResultsUpdater = searchTableVC
let searchBar = searchBarController?.searchBar
searchBar?.sizeToFit()
searchBar?.placeholder = "Digite o local"
searchBar?.tintColor = UIColor.red
searchBar!.isUserInteractionEnabled = true
searchBar?.backgroundColor = UIColor(named: "ColorTransparent")
navigationItem.titleView = searchBarController?.searchBar
searchBarController?.hidesNavigationBarDuringPresentation = false
searchBarController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
If you're using iOS 11, the preferred way is simply to write:
navigationItem.searchController = searchController
That's all. You don't have to position the search bar or put it in any view.
I'm trying to add a UISearchController to a UIViewController that contains a UITableView (and an MKMapView too, but hopefully that's not the problem). I followed Ray Wenderlich's tutorial but I can't get the same result in terms of behaviour.
Here is my viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
// Setup the Search Controller
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = NSLocalizedString("Search references by project, customer or city", comment: "")
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true
} else {
tableView.tableHeaderView = searchController.searchBar
}
definesPresentationContext = true
self.modeSelector.layer.cornerRadius = 5.0
if let split = splitViewController {
let controllers = split.viewControllers
detailViewController = (controllers[controllers.count - 1] as! UINavigationController).topViewController as? ReferenceViewController
}
self.navigationItem.rightBarButtonItem?.isEnabled = false
}
Note that the #available test in the middle is because I need to support iOS up to 9.1.
Now I see several problems:
The search bar appears right away and I can't hide it by scrolling
When I focus the search bar, the top of the tableview doesn't stick to the bottom of the navigation item:
The only major difference I see with Ray Wenderlich's sample project is that since I created my project with Xcode 9, my storyboard doesn't use top and bottom layout guides, but safe areas. Don't know if it's relevant, but that's the only thing I see.
Any idea what's going on and how I could fix this?
If you need to support iOS up to 9.1, you probably use emulator with version older than 9.1. Therefore, "maybe" obscuresBackgroundDuringPresentation doesn't affect the searchController properly, since it's only available on iOS 9.1 or newer. Add dimsBackgroundDuringPresentation to support up to 9.1:
if #available(iOS 9.1, *) {
searchController?.obscuresBackgroundDuringPresentation = false
} else {
searchController?.dimsBackgroundDuringPresentation = false
}
If this doesn't help to display as expected, I'm almost sure that the problem is about your layout constraints. Please add your current constraints if you couldn't bring your layout in compliance with safe area.
use this below line of code into your viewDidload
self.navigationController?.navigationBar.isTranslucent = false
Hope this will help you
If you are using xcode 9 (ios 11). Then the thing you really want to do is -
Use the new Broader navigation bars which are the new highlights in the ios 11 devices. But as there are many who have not shifted to ios-11, previous version devices are also taken into consideration.
For adding the search bar to the newer navigation bars I have used the following function which gives a search bar on scrolling and hides it when user scrolls the page.
func addSearchBar() {
if #available(iOS 11.0, *) {
let sc = UISearchController(searchResultsController: nil)
sc.delegate = self
let scb = sc.searchBar
scb.tintColor = UIColor.white
scb.barTintColor = UIColor.white
//Change the colors as you like them
if let textfield = scb.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;
}
}
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor.white
}
navigationItem.searchController = sc
navigationItem.hidesSearchBarWhenScrolling = true
}else{
//add the logic for previous version devices here.
}
I have also set the
self.navigationController?.navigationBar.prefersLargeTitles = true; in viewDidLoad as there is a bug in xcode9 and setting it from the interface builder does not work(yet).
The following method has been taken from here
I am using UISearchController as part of navigation bar using the new APIs introduced in iOS 11. I am using it in the following manner in my ViewController's viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
[_table setDataSource:self];
[_table setDelegate:self];
searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
[self.navigationItem setSearchController:searchController];
[self.navigationItem setHidesSearchBarWhenScrolling:NO];
[searchController.searchBar setBackgroundColor:[UIColor greenColor]];
}
However, the search text field is rendered at the wrong position inside the search bar. Look at the following screenshot.
https://imgur.com/a/Igf49
I inspected the view hierarchy and found that UISearchBarTextField object (which is not accessible directly to devs) in the search bar has a frame.y value of 1 which is probably causing this issue. I have tested this on iOS 11 beta 10 and iOS 11 GM.
Is this a known issue? Is there any fix for this? Or is it something I am doing wrong on my end?
Any help will be appreciated (:
Here's code which shows how to change the background colour of textField in searchBar on iOS 11.
Before:
After:
Obj-C: in (void)viewDidLoad use this code:
if (#available(iOS 11, *)) {
UITextField *textField = [self.searchController.searchBar valueForKey:#"searchField"];
UIView *backgroundView = textField.subviews.firstObject;
backgroundView.backgroundColor = UIColor.whiteColor;
backgroundView.layer.cornerRadius = 10;
backgroundView.clipsToBounds = YES;
}
Swift: in viewDidLoad() use this code:
if #available(iOS 11.0, *) {
if let textfield = scb.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
if let backgroundview = textfield.subviews.first {
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
}
You image (exact problem) is not accessible. So can see exact problem. But from your statement, I found your problem. I tried following for iOS 11 and its working fine.
My code has a solution to your problem but it is in Swift. You need to convert it into Objective-C. It won't be hard for you to do it.
if #available(iOS 11.0, *) {
let sc = UISearchController(searchResultsController: nil)
sc.delegate = self
let scb = sc.searchBar
scb.tintColor = UIColor.white
scb.barTintColor = UIColor.white
if let textfield = scb.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 = 5;
backgroundview.clipsToBounds = true;
}
}
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor.blue
}
navigationItem.searchController = sc
navigationItem.hidesSearchBarWhenScrolling = false
}
Output: