Scrungepipes
Scrungepipes

Reputation: 37581

How to change the height of a search bar when added as a nav bar button item

I'm adding a search bar to the navigation bar as the left button item. I'm able to set the width of search bar to whatever I want, however the height always comes out the same regardless of what I try and set it to. Here's some code:

var searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width * 0.7, height:28))

let leftNavBarButton                    = UIBarButtonItem(customView:searchBar)  

tabBarController?.navigationItem.leftBarButtonItem   = leftNavBarButton

searchBar.showsCancelButton             = false
searchBar.placeholder                   = NSLocalizedString("VC_SEARCH_PROMPT", comment: "")
searchBar.delegate                      = self
...

I can set the width of the search bar via the width parameter in the UISearchBar constructor, but changing the value for the height parameter has no effect - the height of the search bar is always the same.

Upvotes: 0

Views: 793

Answers (1)

beyowulf
beyowulf

Reputation: 15331

Beginning with iOS 11 you'll need to use constraints for sizing bar button items and title views.

searchBar.heightAnchor.constraint(equalToConstant: 28).isActive = true
searchBar.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.7).isActive = true // doubt this is the actual constraint you want though

This was announced here.

Upvotes: 1

Related Questions