I need to change the orientation of a search bar in a navigation controller to be set to the center. I have created the search bar programmatically with this code:
lazy var searchBar:UISearchBar = UISearchBar(frame: CGRectMake(100, 40, 440, 40))
and in viewDidLoad:
searchBar.placeholder = "Search for Places"
var leftNavBarButton = UIBarButtonItem(customView:searchBar)
self.navigationItem.leftBarButtonItem = leftNavBarButton
However I also have a barButtonItem in the left. This has been set through the interface.
The issue is that when I add the search bar through the code, the screen does not display the button as it gets covered as shown in the screenshot below.
How can I shift the search bar to show the menu button followed by the search bar in the center?
the reason are you assigned the searchBar to leftBarButtonItem that the reason it shows in left side , if you need in center of Navigation add your search to titleView for navigation bar, change this
searchBar.placeholder = "Search for Places"
var leftNavBarButton = UIBarButtonItem(customView:searchBar)
self.navigationItem.leftBarButtonItem = leftNavBarButton
to
lazy var searchBar:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 440, 40))
searchBar.placeholder = "Search for Places"
self.navigationItem.titleView = searchBar // or use self.navigationcontroller.topItem?.titleView = searchBar
Your search bar is on the left because you're setting left navigation button. Try to change titleView instead.
let search = UISearchBar() // Create your search bar
controller.navigationController.navigationItem.titleView = search
Related
lazy var searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width:
self.view.bounds.width - 50, height: 30))
searchBar.delegate = self
searchBar.sizeToFit()
navigationBar.topItem?.titleView = searchBar
A navigation bar in storyboard design has search bar which is made hidden on pushing the viewController, navigation bar on storyboard design is shown when presenting VC. While pushing VC, setting the same search bar to
self.navigationItem.titleView = searchBar
also tried,
navigationController?.navigationBar.topItem?.titleView = searchBar
titleView displays on viewDidLoad() and viewDidAppear(), but when entered background and returned back to app the search bar disappears.
Use UISearchController for UISearchBar. Like,
class ViewController: UITableViewController, UISearchResultsUpdating {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
self.definesPresentationContext = true
// Place the search bar in the navigation item's title view.
self.navigationItem.titleView = searchController.searchBar
// Don't hide the navigation bar because the search bar is in it.
searchController.hidesNavigationBarDuringPresentation = false
}
}
You can get more Information here.
I hope this will help you.
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.
How make animation like ios default mail. I need the same effect like search bar is hide at initial and when i drag table view download the it shows the search bar.
like in the screenshot
update to the question.- Right now i am using a searchBar above tableview in the view controller.
var resultSearchController = UISearchController()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
workActivityTableView.tableHeaderView = controller.searchBar
workActivityTableView.contentOffset = CGPoint(x: 0, y: controller.searchBar.frame.height)
navigationController?.extendedLayoutIncludesOpaqueBars = true
return controller
})()
Drag search bar in to your tableview in your storyboard as it should be the first subview of your tableview!
Tableview will consider it as a header.
Your view hierarchy will be look like,
And the default size of search bar will be 44.
Now in your viewDidload set content offset of your table view like,
_tblView.contentOffset = CGPointMake(0, 44);
or in swift you can say,
tblView.contentOffset = CGPoint(x: 0, y: 44)
and you're done!
bellows are screenshot of results,
Initially
After scroll
This will not work if prefersLargeTitles set to true then initially you will get search bar!
I'd suggest you add the search bar as a tableHeaderView to your tableView, and in your viewDidLoad, add the following lines:
tableView.tableHeaderView = your_searchbar
tableView.contentOffset = CGPoint(x: 0, y: your_searchar.frame.height)
navigationController?.extendedLayoutIncludesOpaqueBars = true
UPDATE:
This can be easily achieved by adding the search controller as a part of navigationItem. Something like this:
self.navigationItem.searchController = searchController
This is available from iOS 11.
I'm trying to add an image to a left on a Navigation Bar using leftBarButtonItem that will serve as a small logo. I'm able to add the image perfectly fine. My question is how do I disable it from being clickable and just keep the same state so it doesn't turn gray when someone presses it. I just want it as an image in the left of the Navigation Bar. Below is what I use to add the logo to the left of the navigation bar. Any help is greatly appreciated.
let btnName = UIButton()
btnName.setImage(UIImage(named: "NavIcon"), forState: .Normal)
btnName.frame = CGRectMake(0, 0, 40, 50)
let leftBarButton = UIBarButtonItem()
leftBarButton.customView = btnName
self.navigationItem.leftBarButtonItem = leftBarButton
btnName.userInteractionEnabled = false
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