Reputation: 381
I am still working on my app and I have a question about the UISearchBar.
Here is my question: How can I get rid of this "Select|Select All| Paste" tool tip appearing in the UISearchBar?
Can any one help me with this? Thank you in advance.
Upvotes: 2
Views: 630
Reputation: 3771
Starting from iOS 7, you can subclass UISearchBar and override this method like this:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}];
return [super canPerformAction:action withSender:sender];
}
This will immediately hide the menu controller and doesn't require to parse UISearchBar subviews.
Upvotes: 1
Reputation: 13364
If you want to hide it only for UISeacrhBar
then:
-(void)setGestures
{
for (id obj in searchBar.subviews)
{
if ([obj isKindOfClass:[UITextField class]])
{
UITextField *textF = (UITextField *)obj;
textF.gestureRecognizers = nil;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(makeSearchBarFirstResponder:)];
textF.gestureRecognizers = @[tapGesture];
}
}
}
-(void)makeSearchBarFirstResponder:(UIGestureRecognizer *)sender
{
[sender.view becomeFirstResponder];
}
And Call [self setGestures]
method in
-(void)viewDidLoad
{
[self setGestures];
}
-(BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
[self setGestures];
return YES;
}
Upvotes: 1
Reputation: 202
may be this code work for you..
first add notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didShowMenu:) name:UIMenuControllerDidShowMenuNotification object:nil];
then implement notification method in which you can hide menu
-(void)didShowMenu:(NSNotification*) notification{
UIMenuController *menuCon = [UIMenuController sharedMenuController];
[menuCon setMenuVisible:FALSE];
}
Upvotes: 0
Reputation: 7226
Here you have to return NO for all the specific methods the menu controller performs.
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(defineSelection:))
{
return NO;
}
else if (action == @selector(translateSelection:))
{
return NO;
}
else if (action == @selector(copy:))
{
return NO;
}
return [super canPerformAction:action withSender:sender];
}
Hope this helps .
Upvotes: 0