2

我仍在开发我的应用程序,我对 UISearchBar 有疑问。在此处输入图像描述 这是我的问题:如何摆脱 UISearchBar 中出现的“选择|全选|粘贴”工具提示?谁能帮我这个?先感谢您。

4

4 回答 4

1

如果你只想隐藏它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;
}
于 2013-08-08T10:37:09.817 回答
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 子视图。

于 2014-09-05T06:38:10.597 回答
0

可能这个代码对你有用..

首先添加通知

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

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

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

}

于 2013-08-08T10:11:06.990 回答
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];
}

希望这可以帮助 。

于 2013-08-08T09:39:07.850 回答