self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 5, self.view.bounds.size.width, 35)];
self.searchBar.delegate = self;
//设置背景色
self.searchBar.barTintColor = [UIColor colorWithRed:239/255.0 green:240/255.0 blue:244/255.0 alpha:1.0];
//设置右边的取消按钮
for (id obj in [self.searchBar subviews]) {
if ([obj isKindOfClass:[UIView class]]) {
for (id obj2 in [obj subviews]) {
if ([obj2 isKindOfClass:[UIButton class]]) {
UIButton *btn = (UIButton *)obj2;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}
}
}
[self.searchBar setBackgroundImage:[UIImage new]]; //去掉多余的黑线
[self.view addSubview:self.searchBar];