How to Change the Default Text of Cancel Button Which Appears in the Uisearchbar +Iphone

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:-

Sample Image

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



Leave a reply



Submit