如何覆盖在的UISearchBar取消按钮(How to override the cancel b

2019-07-04 20:32发布

我目前使用下面的方法来阻止显示在搜索栏中了取消按钮项。 我有一个自定义UIButton ,我想改为使用。

问题是,在目前内置的取消按钮仍然显示出来。

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

谢谢你的帮助

Answer 1:

您可以隐藏你使用这个取消按钮

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
}


Answer 2:

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

一个经过这个环节的UISearchBar禁用取消按钮自动禁用



Answer 3:

Swift 4.24.0+ Rajneesh071答案

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


文章来源: How to override the cancel button in the UISearchBar