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:
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).
Add a subview to the cancel button.
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)
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)
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.
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
Custom Uifont Baseline Shifted
Why Page Push Animation Tabbar Moving Up in the iPhone X
Navigation Pop View When Swipe Right Like Instagram iPhone App.How I Achieve This
Duplicate Symbols for Architecture Arm64
Detect Backspace Event in Uitextfield
Local and Push Notifications in iOS Version Compatible
Creating a Percentage Based iOS Layout
How to Use the Uisearchbar and Uisearchdisplaycontroller
iOS Nsattributedstring on Uibutton
Exc_Bad_Instruction (Code=Exc_I386_Invop, Subcode=0X0) on Dispatch_Semaphore_Dispose
Styling the Cancel Button in a Uisearchbar
Fatal Error: Init(Coder:) Has Not Been Implemented Error Despite Being Implemented
Open Uisplitviewcontroller to Master View Rather Than Detail
Ios: Add Imageview in a Scrollview to Have Zoom
How Shouldchangecharactersinrange Works in Swift
Warning: Output of Vertex Shader 'V_Gradient' Not Read by Fragment Shader