搜索框原生的也可以使用 但是根据设计师的要求呢,原生的往往满足不了需求,这里对搜索框进行简单的改变
首先去掉搜索框的背景
for (UIView *view in _searchBar.subviews) {
// for before iOS7.0
if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[view removeFromSuperview];
break;
}
// for later iOS7.0
if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {
[[view.subviews objectAtIndex:0] removeFromSuperview];
break;
}
}
改变搜索框内部占位字符的颜色和大小
UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
searchField.placeholder = @"哈哈哈哈";
searchField.textColor = [UIColor whiteColor];
searchField.font = [UIFont systemFontOfSize:12];
[searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
上面是改变占位字符的颜色以及大小
下边是改变搜索按钮的左边放大镜图片
UIImage *image = [UIImage imageNamed:@"searchs"];
UIImageView *iView = [[UIImageView alloc] initWithImage:image];
searchField.leftView = iView;