设置UISearchBar

更改searchBar的背景颜色及搜索框的颜色

- (UISearchBar *)searchBar{

if (!_searchBar) {

_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 14.5, self.view.frame.size.width, 46.5)];

_searchBar.searchBarStyle = UISearchBarStyleProminent;

_searchBar.placeholder = @"xxxxx";

[_searchBar setBackgroundColor:[UIColor whiteColor]];

[[[_searchBar.subviews objectAtIndex:0].subviews objectAtIndex:0] removeFromSuperview];

for (UIView *view in [_searchBar.subviews firstObject].subviews) {

if ([view isKindOfClass:[UITextField class]]) {

[(UITextField *)view addTarget:self action:@selector(searchBarSearchAction:) forControlEvents:UIControlEventEditingChanged];

[(UITextField *)view setBackground:nil];

view.backgroundColor = COLORRGB(247, 247, 247);

}

}

[_searchBar setShowsCancelButton:YES];

for (UIView *cancelBT in [_searchBar.subviews lastObject].subviews) {

//设置取消按钮

if ([cancelBT isKindOfClass:[UIButton class]]) {

UIButton *btn = (UIButton *)cancelBT;

[btn setTitle:@"取消" forState:(UIControlStateNormal)];

btn.titleLabel.font = [UIFont systemFontOfSize:16];

btn.enabled = YES;//使其不在搜索状态是也可以更改

}

}

_searchBar.delegate = self;

}

return _searchBar;

}

你可能感兴趣的:(设置UISearchBar)