Igor Voitenko
Igor Voitenko

Reputation: 305

Make SearchBar higher in navigation bar

I tried to create a search bar and it's working ok now, but I can't figured out how to make it a little bit higher, right under the status bar. Search bar is created programmatically, I used this code:

let searchController = UISearchController(searchResultsController: nil)




override func viewDidLoad() {
    super.viewDidLoad()

    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.hidesNavigationBarDuringPresentation = false
    definesPresentationContext = true
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false
}

Search bar is too low

Upvotes: 1

Views: 86

Answers (1)

Harshal Yanpallewar
Harshal Yanpallewar

Reputation: 2122

You mean that, you want a search bar on the place of Navigation Bar. If this is the case the try to add search bar in navigation bar.

Try this

var leftNavBarButton = UIBarButtonItem(customView:Yoursearchbar)
self.navigationItem.leftBarButtonItem = leftNavBarButton

You keep a lazy UISearchBar property

lazy var searchBar:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 200, 20))

In viewDidLoad

searchBar.placeholder = "Your placeholder"
var leftNavBarButton = UIBarButtonItem(customView:searchBar)
self.navigationItem.leftBarButtonItem = leftNavBarButton

If you want to use storyboard just drag your searchbar as a outlet,then replace the lazy property with your outlet searchbar

OR Simply

// create the search bar programatically since you won't be
// able to drag one onto the navigation bar


searchBar = UISearchBar()  
searchBar.sizeToFit()


// the UIViewController comes with a navigationItem property
// this will automatically be initialized for you if when the
// view controller is added to a navigation controller's stack
// you just need to set the titleView to be the search bar

navigationItem.titleView = searchBar

Upvotes: 1

Related Questions