在UISearchBarDelegate代理方法:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;
设置显示取消按钮
[searchBar setShowsCancelButton:YES animated:YES];
通过遍历searchBar的所有子控件,找到UIButton的子类的哪个button进行属性设置。
示例代码
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
for (UIView *searchViews in searchBar.subviews) {
for (UIView *view in searchViews.subviews) {
//是按钮
if ([view isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)view;
[button setTitleColor:AB_Color_ff5400 forState:UIControlStateNormal];
[button setTitleColor:AB_Color_ff5400 forState:UIControlStateHighlighted];
button.titleLabel.font = AB_FONT_17;
}
}
}
}