Styling the Cancel Button in a Uisearchbar

Modifying UISearchBar Cancel button font text color and style

You can change Cancel button styling by changing the appearance of UIBarButtonItem when contained in UISearchBar.

For example,

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blueColor],
UITextAttributeTextColor,
[UIColor darkGrayColor],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
nil]
forState:UIControlStateNormal];

Styling the cancel button in a UISearchBar

What you want to do is pretty tough. There is no built-in hook to get at the cancel button.

However, there are a couple of options if you are willing to jimmy open the hood.

First off, UISearchBar is a UIView, and the Cancel button is also a view, which is added into the search bar as a subview, just as you would expect.

I have experimented a little, and can tell you that when the button is onscreen it has a size of 48,30.

So in viewWillAppear, you can do something like this:

  1. Find the cancel button view in [searchBar subviews] by looking for one with size 48,30. (There only seems to be one -- this could change...) You could be doubly careful and look for one that is in approximately the correct position (differs in landscape and portrait).

  2. Add a subview to the cancel button.

  3. The subview should be a UIControl (so that you can set enabled = NO, in order to make sure touch events get to the actual cancel button)

  4. It needs to have the right color and rounded corners; you will need to fudge the size for reasons I don't yet understand (55,30 seems to work)

  5. This will work if searchBar.showsCancelButton is always YES; if you want it to disappear when not editing the search string, you will need to find a hook to add the overlay each time the cancel button appears.

  6. As you can see, this is some ugly tinkering. Do it with eyes wide open.

Changing the font style for UISearchBar cancel button

Try something like this it worked for me.

 UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont(name: "OpenSans", size: 15)!], forState: .Normal)

If you want to change the color as well add this to your array of attributes.

 NSForegroundColorAttributeName : UIColor.whiteColor()

Note: Made available in iOS 9

How to change the colour of the 'Cancel' button on the UISearchBar in Swift

Having a look around, this seemed to be the best way to achieve what I needed:

 let cancelButtonAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes , for: .normal)

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

Access the cancel button as follows:-

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

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

Want to change the position of the cancel button of the UISearchBar

You cannot change the position of the default SearchBar and Search Display controller.

For your requirement, you need to create your own custom Search Bar with Cancel as you desire.

Hope it helps..

UISearchBar set different tint color for cancel button

i found the solution:

let view: UIView = self.searchBar.subviews[0] as UIView
let subViewsArray = view.subviews

for subView: UIView in subViewsArray {
if subView.isKindOfClass(UITextField){
subView.tintColor = UIColor.blackColor()
}
}


Related Topics



Leave a reply



Submit