I customly add UISearchBar controller in the navigationItem. And it works fine on iphone but giving issue on ipad.
I am using a container view under the navigation bar on which i am calling different view controllers. But as soon as i select the search bar the search bar goes up to hide the status bar but the container view under the navigation didn't shift up.
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.placeholder = "Search"
if let textfield = self.searchController.searchBar.value(forKey: "searchField") as? UITextField {
if let backgroundview = textfield.subviews.first {
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
self.searchController.searchBar.tintColor = UIColor.cityworksBlue()
if #available(iOS 11.0, *) {
self.searchController.searchBar.showsScopeBar = false;
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.hidesNavigationBarDuringPresentation = true
navigationItem.searchController = self.searchController
navigationItem.hidesSearchBarWhenScrolling = false
}
What i want to do is if search bar shifts upwards then container view should also shift upward. It works in iphones but giving problem in ipad.
I found my solution by selection "Under Top Bars" option.
Related
HI i have implemented Searchbar in Navigationbar and
Hiding Navigationbar Progamatically but the space is not removing
PLease help how can i remove the space of searchbar hiding space
let search = UISearchController(searchResultsController: nil)
search.searchResultsUpdater = self
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false;
search.searchBar.placeholder = "search..."
self.definesPresentationContext = true
self.navigationItem.searchController = search
for Hiding the SearchBar
search.searchBar.isHidden = false
Hiding it won't be enough because it is still the navigationItem searchController, so you need to set it to nil
self.navigationItem.searchController = nil
and later restore it if you want, simple as that.
I am running into a small problem, I implemented the new iOS 11's style search bar in my app, and I noticed that it disappeared with a slightly different animation from the one in Messages for example. It's faster and less smooth.
Anyone has ever stumble upon this "problem" ?
Here is the code I use :
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.barTintColor = .white
searchController.searchBar.layer.borderColor = UIColor.white.cgColor
searchController.searchBar.layer.borderWidth = 1
}
definesPresentationContext = true
searchController.searchBar.placeholder = "all_search".localized
EDIT:
I don't know if it will help you, but I am scrolling at a normal pace .
Thanks
Adding
self.extendedLayoutIncludesOpaqueBars = true
to my viewDidLoad solved the issue, your navigation bar must not be translucent and note that extendedLayoutIncludesOpaqueBars = true is being attributed to my main view which holds the tableview.
This happens when your table view doesn't go all the way to the top of the view. Make sure your table view is "behind" the navigation bar and uses extended edges and extend under opaque edges if your navigation bar is opaque.
Try this, it fixes it for me. I used a different UIViewController as the searchResultsUpdater and just set extendedLayoutIncludesOpaqueBars as true.
searchResultsUpdater.extendedLayoutIncludesOpaqueBars = true
searchController.searchResultsUpdater = searchResultsUpdater
UIView.animate(withDuration: 1, animations: {
//your codes to implement
}, completion: nil)
change withDuration: in seconds
I placed searchController's searchBar in navigationItem as the titleView as follows:
self.navigationItem.titleView = self.searchController.searchBar;
Although it fits perfectly when it is not selected (cancel item is not shown), if it becomes first responder and cancel item appears next to the searchBar, it slides under of the navigationItem slightly. You can see the result below:
How can I fix this? Is there a workaround?
You should use self.navigationItem.searchController instead of self.navigationItem.titleView.
self.navigationItem.searchController = self.searchController;
Learn more here https://developer.apple.com/documentation/uikit/uinavigationitem/2897305-searchcontroller
Use below code
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
// Fallback on earlier versions
navigationItem.titleView = searchController?.searchBar
}
I'm having an issue with UISearchController. There have been some reports of erratic behavior in iOS 11, but none of the suggestions have fixed my problem.
The navigation bar is hidden in my app so I just want to place the search bar between two buttons on the main screen. I put UIView in the storyboard to serve as the superview for the search bar. When activated the results controller is a straight UITableViewController.
Everything is in-place when the app launches. When I access the search bar it just to the top of the screen, leaving it's parent view behind. Everything functions ok, but when I hide the table view, the search bar actual goes a bit lower than it was when it started. Here's the setup code:
let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTable") as! LocationSearchTable
searchController = UISearchController(searchResultsController: locationSearchTable)
searchController.searchResultsUpdater = locationSearchTable
searchController.delegate = self
locationSearchTable.mapView = mapView
locationSearchTable.handleMapSearchDelegate = self
locationTableController = locationSearchTable
let searchBar = searchController!.searchBar
//searchBar.searchBarStyle = UISearchBarStyle.prominent
searchBar.sizeToFit()
searchBar.placeholder = "Search..."
searchViewHolder.addSubview(searchBar)
searchBar.frame = searchViewHolder.bounds
searchBar.autoresizingMask = [.flexibleWidth, .flexibleHeight]
searchController.dimsBackgroundDuringPresentation = true
definesPresentationContext = false
I have tried many, many approaches to fixing this. I'm wondering if I misunderstood an earlier suggestion. Any advice is welcome.
I'd the same issue. If your navbar is not translucent try to put this in viewDidLoad:
extendedLayoutIncludesOpaqueBars = true
I tried to implement search bar in UIViewController by embedding UISearchBar as subview in navigationItem title view. After implementing, i am seeing some space before search bar in navigation.
Code i added to embed search bar in navigation title :
let searchBar = self.searchBar!
searchBar.showsCancelButton = true
searchBar.sizeToFit()
searchBar.delegate = self;
searchBar.barTintColor = UIColorFromRGB(0xCFDFE7)
searchBar.clipsToBounds = true
searchBar.layer.cornerRadius = 6
searchBar.layer.borderWidth = 1.0
searchBar.layoutIfNeeded()
if let button = self.getCancelButtonFromSearchBarView(searchBar) {
button.setTitle("Close", forState: UIControlState.Normal)
}
var barWrapper = UIView(frame:searchBar.bounds)
barWrapper.addSubview(searchBar)
self.navigationItem.titleView = barWrapper
Search bar appearance in view controller : space is marked in red color on left side of search bar.
Could someone suggest how can i adjust search bar to left without space?
From comment of #harish,
UISearchBar(frame: CGRectMake(-5, 0, 320, 44)) use this tips might be it will help you – harish