Vladimir Vishnyagov
Vladimir Vishnyagov

Reputation: 83

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

Upvotes: 2

Views: 1336

Answers (3)

Aleksey Shevchenko
Aleksey Shevchenko

Reputation: 1241

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.

Upvotes: 4

pkc
pkc

Reputation: 8512

Access the cancel button as follows:-

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

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

Upvotes: 4

Abdelahad Darwish
Abdelahad Darwish

Reputation: 6067

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

Upvotes: 0

Related Questions