dan
dan

Reputation: 77

searchBar not centered in navigationItem

Current status Current Status

I am doing it programmatically. I also tried to set it into TitleView, but it's not working. Code below:

        //searchBar
    let searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search Players"
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false
    definesPresentationContext = true

I also have a ScopeBar under the searchBar, when selected. If I do it like:

searchController.titleView = searchController.searchBar 

It goes in place, but I cannot select/tap the searchBar anymore.

Upvotes: 1

Views: 505

Answers (3)

One way to go would be using the bounds to set it to center manually. But on the searchbar activation(click) it would be again messed up and needed to be set again.

searchController.searchBar.bounds=CGRectInset(searchController.searchBar.frame, 0.0f, -2.5f);

Upvotes: 0

Cenk Işık
Cenk Işık

Reputation: 85

if your iOS version greater than ios11 or equal you should add like this

navigationItem.searchController = searchController

else

navigationItem.titleView = searchController.searchBar

and also you should give more detail

Upvotes: 0

Anton Rodzik
Anton Rodzik

Reputation: 791

For me, your example works well, but maybe you canuse it.

   navigationItem.titleView = searchController.searchBar

You can insert search bar into navigation like here. Can you explain what's wrong with search bar in table view header? What isn't work?

Upvotes: 0

Related Questions