如何删除UISearchBar中的工具提示

时间:2013-08-08 09:05:24

标签: iphone objective-c xcode uisearchbar

我仍在处理我的应用,我对UISearchBar有疑问。enter image description here 这是我的问题:如何摆脱出现在UISearchBar中的“选择|全选|粘贴”工具提示? 谁能帮我这个?提前谢谢。

4 个答案:

答案 0 :(得分:1)

从iOS 7开始,您可以继承UISearchBar并覆盖此方法,如下所示:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
    }];
    return [super canPerformAction:action withSender:sender];
}

这将立即隐藏菜单控制器,并且不需要解析UISearchBar子视图。

答案 1 :(得分:0)

在这里,你必须为菜单控制器执行的所有特定方法返回 NO

-(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];
}

希望这有帮助。

答案 2 :(得分:0)

可能是这段代码适合你..

首先添加通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didShowMenu:) name:UIMenuControllerDidShowMenuNotification object:nil];

然后实现通知方法,您可以在其中隐藏菜单

-(void)didShowMenu:(NSNotification*) notification{
UIMenuController *menuCon = [UIMenuController sharedMenuController];
[menuCon setMenuVisible:FALSE];

}

答案 3 :(得分:0)

如果您只想为UISeacrhBar隐藏它,那么:

-(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];
}

并在

中调用[self setGestures]方法
-(void)viewDidLoad
{
   [self setGestures];
}
-(BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
   [self setGestures];
   return YES;
}