Why won't my UISearchBar obey constraints? - ios

In my iOS app I have a navigation bar composed of a UISearchBar and a UIButton in a UIStackView. Since I can't storyboard a UISearchController I have a blank UIView in the stack view and I'm adding the UISearchBar as a subview. Here's what my storyboard looks like:
Here's my code to add the search bar
override func viewDidLoad() {
super.viewDidLoad()
configureSearchController()
print(wrapperView.bounds)
print(searchController.searchBar.bounds)
}
func configureSearchController() {
searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
searchController.searchBar.sizeToFit()
searchController.searchBar.isTranslucent = false
searchController.searchBar.delegate = self
searchController.delegate = self
searchController.searchBar.barTintColor = addButton.backgroundColor
wrapperView.addSubview(searchController.searchBar)
let color = addButton.backgroundColor
searchController.searchBar.layer.borderWidth = 1
searchController.searchBar.layer.borderColor = color?.cgColor
searchController.searchBar.trailingAnchor.constraint(equalTo: addButton.leadingAnchor)
}
But the search bar goes past the UIButton, as shown here:
How can I get the UISearchBar to end at the UIButton like this?

Try replacing:
searchController.searchBar.trailingAnchor.constraint(equalTo: addButton.leadingAnchor)
with:
searchController.translatesAutoresizingMaskIntoConstraints = false
wrapperView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: nil, metrics: nil, views: ["subview": searchController]))
wrapperView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: nil, metrics: nil, views: ["subview": searchController]))
EDIT:
Otherwise try with a UISearchController:
searchController.searchBar.setImage(<image_name>, forSearchBarIcon: .Bookmark, state: .Normal)
this will allow you to add a custom button near the search bar.

I experienced the same issue, and I was able to fix it by calling searchController.searchBar.sizeToFit() after adding it to my view (right after the addSubview() line). You might have to run wrapperView.layoutIfNeeded() before, so something like this:
wrapperView.addSubview(searchController.searchBar)
wrapperView.layoutIfNeeded()
searchController.searchBar.sizeToFit()
Hope it helps!

Related

How to fix the position of searchbar us

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

Status Bar becomes white in iOS when using UISearchController in iOS 13

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")
}
}

Unable to change height of search bar and textfield inside it

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.

How to remove shadow below UINavigationBar with UISearchController

I could successfully remove the shadow below the navigation bar with the following line of code.
self.navigationController?.navigationBar.shadowImage = UIImage()
When I added a search controller however, the shadow reappeared.
self.navigationItem.searchController = UISearchController(searchResultsController: nil)
I tried the following, but resulted an unexpected behavior.
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.barTintColor = .white
self.navigationController?.navigationBar.isTranslucent = false
How do I remove the shadow under a navigation bar when there is a search controller attached?
I have not found good solution too...
for now I will hide it this way:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let imageView = navigationItem.searchController?.searchBar.superview?.subviews.first?.subviews.first as? UIImageView {
imageView.isHidden = true
}
}
Here's the solution that I use
Create a separate class extending a UINavigationController instance, lets call it BaseNavigationController. Here's your class for you
If you are using storyboards, assign BaseNavigationController to your UINavigationController scene in the storyboard
If you are initializing a navigation controller via code, ex: UINavigationController.init(rootViewController: someViewControllerInstance) then simply use BaseNavigationController.init(rootViewController: someViewControllerInstance)
An example of the class is shown below:
open class BaseNavigationController:UINavigationController {
override open func viewDidLoad() {
super.viewDidLoad()
setNavigationBar()
setNavBarBorder(false)
}
func setNavigationBar(color:UIColor?=UIColor.white, tint:UIColor?=UIColor.darkGray){
let appearance = UIBarButtonItem.appearance()
appearance.setBackButtonTitlePositionAdjustment(UIOffset.init(horizontal: 0.0, vertical: 0), for: .default)
self.navigationBar.barTintColor = color!
self.navigationBar.isTranslucent = false
self.navigationBar.tintColor = tint!
self.navigationBar.titleTextAttributes = [ NSAttributedString.Key.foregroundColor: tint! ]
}
func setTitleColor(_ color:UIColor?=UIColor.darkGray){
}
func setNavBarBorder(_ enable:Bool) {
self.navigationBar.setBackgroundImage((enable ? nil : UIImage()), for: UIBarMetrics.default)
self.navigationBar.shadowImage = (enable ? nil : UIImage())
self.navigationBar.setValue(true, forKey: "hidesShadow")
}
}
Now, the fun part, if you are handling ambiguous layout you might need to do this in viewWillLayoutSubviews otherwise putting this bit of code in viewWillAppear
of the viewController instance.
(self.navigationController as? BaseNavigationController)?. setNavBarBorder(false)
the interesting bit of code is in self.navigationBar.setValue(true, forKey: "hidesShadow"), for some versions of iOS.
You can instead add searchBar to your viewController in storyBoard and set its Search Style property to Minimal and it'll be look like this:
My little contribution about this issue. Of course, it is not the right way to do it but it's working for opaque navigation bar. The idea is to add a view on top of this shadow.
let searchBar = searchController.searchBar
let maskView = UIView(frame: .zero)
maskView.backgroundColor = .white // or any color you want
searchBar.addSubview(maskView)
//We need to use constraint so the mask view follow the search bar animation
maskView.translatesAutoresizingMaskIntoConstraints = false
let views = ["maskView": maskView]
searchBar.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(0)-[maskView]-(0)-|",
options: NSLayoutConstraint.FormatOptions.alignAllCenterY,
metrics: nil,
views: views))
searchBar.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[maskView(1)]-(-1)-|",
options: NSLayoutConstraint.FormatOptions.alignAllCenterX,
metrics: nil,
views: views))

UISearchController does not display as expected

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

Resources