如何覆盖UISearchBar中的取消按钮

3

我目前使用以下方法,以防止在搜索栏中出现取消按钮项。我有一个自定义的UIButton,我想要使用它来替代取消按钮。

问题是,目前内置的取消按钮仍然会出现。

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
    controller.searchBar.showsCancelButton = NO;
}

感谢帮助。
谢谢。

你是通过编程还是通过XIB添加搜索栏? - Master Stroke
3个回答

3
你可以使用以下方法隐藏取消按钮。
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
}

0
for (UIView *possibleButton in searchBar.subviews)
{  
    if ([possibleButton isKindOfClass:[UIButton class]])  
    {  
        UIButton *cancelButton = (UIButton*)possibleButton;  
        cancelButton.enabled = YES;  
        break;
    }    
}

通过此链接 禁用UISearchBar自动禁用取消按钮


0

Swift 4.2,4.0+Rajneesh071答案

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchBar.setShowsCancelButton(false, animated: true)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接