2

I moved the search bar from UIView to navigationItem in UIViewController when I was updating the application from 10 to 11 iOS SDK. Further, I set to position a background image in UISearchBar with a vertical offset because the distance between the logo and searchBar was small.

search is not active

searchBar.setSearchFieldBackgroundImage(searchFieldImage, for: .normal)
searchBar.searchFieldBackgroundPositionAdjustment = UIOffset(horizontal: 0, vertical: 4)

But I can't right be positioning the Cancel button on vertical. How can I set the vertical position the Cancel button in UISearchBar, or title in button?

search is active

3 Answers3

4

Access the cancel button as follows:-

UIBarButtonItem *cancelButton = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];

Now try to update the frame(y axis) of cancelButton.

pkc456
  • 8,350
  • 38
  • 53
  • 109
2
let cancelButton = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
let offset = UIOffset(horizontal: 0, vertical: 7)
cancelButton.setTitlePositionAdjustment(offset, for: .default)

Also take a look at apple doc here.

Aleksey Shevchenko
  • 1,159
  • 1
  • 11
  • 23
0

You can increase searchBar Height heightAnchor.constraint to increase navigation Bar Height to fix image logo issue , check it

In IOS 11

searchController.searchBar.heightAnchor.constraint(equalToConstant: 50).isActive = true
Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35