How to change the default text of Cancel Button which appears in the UISearchBar +iPhone
You also need to have the "searchBar setShowsCancelButton" before the procedure.
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
[theSearchBar setShowsCancelButton:YES animated:NO];
for (UIView *subView in theSearchBar.subviews){
if([subView isKindOfClass:[UIButton class]]){
[(UIButton*)subView setTitle:@"Done" forState:UIControlStateNormal];
}
}
}
Note also: use UIButton to avoid problems with Apple!
iOS Change the title of cancel button in UISearchBar
simply do this code for it:-
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
/* when user start editing in serchbar this method will display cancel button and disable the autocorrection functionality */
srcbar.showsCancelButton = YES;
for (UIView *subView in searchBar.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *cancelButton = (UIButton*)subView;
[cancelButton setTitle:@"hi" forState:UIControlStateNormal];
}
}
srcbar.autocorrectionType = UITextAutocorrectionTypeNo;
}
Not test in iOS7 but this working fine in iOS6 hope this working for you.
OUTPUT IS:-
UISearchController change 'Cancel' button title
You can change the "Cancel" Button in search bar using this-
for (UIView *view in searchBar.subviews)
{
for (id subview in view.subviews)
{
if ( [subview isKindOfClass:[UIButton class]] )
{
[subview setEnabled:YES];
UIButton *cancelButton = (UIButton*)subview;
[cancelButton setTitle:@"hi" forState:UIControlStateNormal];
NSLog(@"enableCancelButton");
return;
}
}
}
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..
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];
iOS - Customizing Cancel button of UISearchBar
You could search for UISearchBar
subViews and locate the cancel button, it is dangerous to do so, since the button could change
For example you could add this in your viewWillAppear
- (void) viewWillAppear:(BOOL)animated
{
//show the cancel button in your search bar
searchBar.showsCancelButton = YES;
//Iterate the searchbar sub views
for (UIView *subView in searchBar.subviews) {
//Find the button
if([subView isKindOfClass:[UIButton class]])
{
//Change its properties
UIButton *cancelButton = (UIButton *)[sb.subviews lastObject];
cancelButton.titleLabel.text = @"Changed";
}
}
}
As i said before this could change, its a hack to do so, you better stick with the original, or create your own search bar.
Cancel Button in UISearchBar issue
Try setting your NSLocalizedString
with UIAppearance
:
[[UIButton appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:Localized(@"Cancel") forState:UIControlStateNormal];
swift
UIButton.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitle("v1_cancel", for: .normal)
(which no longer works in modern ios)
this one
Change UISearchBar cancel button text in iOS 8
is up to date
Related Topics
Paging Uiscrollview in Increments Smaller Than Frame Size
iPhone Sdk:How to Play Video Inside a View? Rather Than Fullscreen
Rotating a View in Layoutsubviews
Navigationbar Bar, Tint, and Title Text Color in iOS 8
How to Get Navigation Based Template Functionality in Swift Programming
Migrating from Uiwebview to Wkwebview
Catch an Exception for Invalid User Input in Swift
Communicating Between iOS and Android with Bluetooth Le
Downloading Uiimage via Alamofireimage
How to Change the Colors of a Segment in a Uisegmentedcontrol in iOS 13
Maintaining Good Scroll Performance When Using Avplayer
Getting Back a Date from a String
Uicollectionview - Horizontal Scroll, Horizontal Layout
Why Is Uiscrollview Leaving Space on Top and Does Not Scroll to the Bottom