我目前使用以下方法,以防止在搜索栏中出现取消按钮项。我有一个自定义的UIButton
,我想要使用它来替代取消按钮。
问题是,目前内置的取消按钮仍然会出现。
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
controller.searchBar.showsCancelButton = NO;
}
感谢帮助。
谢谢。
我目前使用以下方法,以防止在搜索栏中出现取消按钮项。我有一个自定义的UIButton
,我想要使用它来替代取消按钮。
问题是,目前内置的取消按钮仍然会出现。
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
controller.searchBar.showsCancelButton = NO;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
}
for (UIView *possibleButton in searchBar.subviews)
{
if ([possibleButton isKindOfClass:[UIButton class]])
{
UIButton *cancelButton = (UIButton*)possibleButton;
cancelButton.enabled = YES;
break;
}
}
通过此链接 禁用UISearchBar自动禁用取消按钮
Swift
4.2,4.0+ 的Rajneesh071答案
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.setShowsCancelButton(false, animated: true)
}