Search code examples
iosswiftuisearchbarios11uisearchcontroller

Is it possible to change the vertical position of cancel button in the search bar?


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


Solution

  • Access the cancel button as follows:-

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

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