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.
Related
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 am programming an app in Swift 2 on XCode 7.3 for iOS 9.3, and I have a search controller which is part of a navigation controller. I want to hide the navigation bar for this specific view, but when I do the background color of the status bar becomes white on the very top of the screen, and I want to keep it colored. Is there a way to do so while also hiding the navigation bar?
Here's an image for reference: Search View
self.searchController = UISearchController(searchResultsController: searchResultsController)
self.searchController.searchResultsUpdater = searchResultsController
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.searchBar.searchBarStyle = .Minimal
for hide status bar for particuler scrren:
override func viewWillDisappear(animated: Bool) {
UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: .None)
}
override func viewWillAppear(animated: Bool) {
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: .None)
}
for navigation embeded with search controller use below code:
self.searchController = UISearchController(searchResultsController: searchResultsController)
self.searchController.searchResultsUpdater = searchResultsController
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.searchBar.searchBarStyle = .Minimal
The background color of the status bar becomes white on the very top of the screen and NavigationBar is Hide at that time solution is add one sub view with background color in ViewController's viewDidLoad method. Add following code in ViewController's viewDidLoad method.
let viewStatusBarBG = UIView(frame: CGRectMake(0, 0, self.view.frame.width, 20))
viewStatusBarBG.backgroundColor = UIColor(red: 0.89, green: 0.22, blue: 0.37, alpha: 1.0)
self.view.addSubview(viewStatusBarBG)
Set backgroundColor whatever you want.
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
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
My UITableViewController is conforming to the new UISearchControllerDelegate and also UISearchResultsUpdating.
Here is my setup code for the search bar:
override func viewDidLoad() {
var searchController = UISearchController(searchResultsController: self)
searchController.searchResultsUpdater = self
self.tableView.tableHeaderView = searchController.searchBar
self.definesPresentationContext = true
}
However, when running this in the simulator there is no search bar in the table header, even though it is specified in the code. I also tried this code in viewWillAppear, but again no search bar was shown.
I was informed by an Apple Engineer that you must give the Search Bar a frame. If you print the frame of the search bar, you will notice it's height is zero. So this is probably a bug in Apple's code.
searchController.searchBar = CGRectMake(0.0, 0.0, 320.0, 44.0)
Edit:
The documentation specifies that you must pass in the View Controller that you want to display the results. To display this in the same View Controller you are in, pass in nil.
var searchController = UISearchController(searchResultsController: nil)