Uisearchbar's Cancel and Clear Buttons Not Working in iOS 7

Cancel button is not shown in UISearchBar

iOS7 does not show the cancel button when added to a navigation bar.You can put searchbar in another view like this.

UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = YES;
[searchBar sizeToFit];
UIView *viewForSearchBar = [[UIView alloc]initWithFrame:searchBar.bounds];
[viewForSearchBar addSubview:searchBar];
self.navigationItem.titleView = viewForSearchBar;

Cancel and Clear button behaving differently in iOS 6 and iOS 7

My Bad.
In one of the implementation files a category was written on UIButton purposefully. See code below:

@implementation UIButton(PassTapGesture)

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
@end

I just removed above code and everything is charm in iOS 7 too. Still I am confused that with the same code why it was working in iOS 6.

UISearchBar cancel button not responding

This is search bar cancel button's default behaviour. If you want other functionality, you can just uncheck cancelbutton property for search bar and can use UIButton as cancel button.

iOS 7 Search Bar - Cancel button doesn't work

With this function I resolved the problem:

-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
controller.active = YES;

[self.view addSubview:controller.searchBar];
[self.view bringSubviewToFront:controller.searchBar];
}

Hope it helps!

iOS7 when UIsearchbar added in UINavigationBar not showing cancel button

For some reason iOS7 does not show the cancel button when added to a navigation bar. This also happens if you try setting it as the titleView of a navigationItem.

You can circumvent this problem by wrapping the UISearchBar in another UIView first. Here's how I do it as a titleView:

UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = YES;
[searchBar sizeToFit];
UIView *barWrapper = [[UIView alloc]initWithFrame:searchBar.bounds];
[barWrapper addSubview:searchBar];
self.navigationItem.titleView = barWrapper;

Cancel button is not shown in UISearchBar

iOS7 does not show the cancel button when added to a navigation bar.You can put searchbar in another view like this.

UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = YES;
[searchBar sizeToFit];
UIView *viewForSearchBar = [[UIView alloc]initWithFrame:searchBar.bounds];
[viewForSearchBar addSubview:searchBar];
self.navigationItem.titleView = viewForSearchBar;

Cancel and Clear button behaving differently in iOS 6 and iOS 7

My Bad.
In one of the implementation files a category was written on UIButton purposefully. See code below:

@implementation UIButton(PassTapGesture)

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
@end

I just removed above code and everything is charm in iOS 7 too. Still I am confused that with the same code why it was working in iOS 6.

Remove clear button (grey x) to the right of UISearchBar when cancel button tapped

The problem is that UISearchBar doesn't expose it's text field, and manages the properties on the text field itself. Sometimes, the values of the properties aren't what you want.

For instance, in my own app, I wanted the keyboard style for my search bar to use the transparent alert style.

My solution was to walk through the subviews of the search bar until you find the text field. You should then be able to set the clearButtonMode property, using something like UITextFieldViewModeWhileEditing as a parameter.

This should make it so that the clear button is only shown while the text field is editing.

You want to do this on viewDidLoad or something early, so it's set before you start using it (but after the search bar is initialised.

for (UIView *subview in searchBar.subviews)
{
if ([subview conformsToProtocol:@protocol(UITextInputTraits)])
{
[(UITextField *)subview setClearButtonMode:UITextFieldViewModeWhileEditing];
}
}


Related Topics



Leave a reply



Submit