搜索框

UIView* searchview=[[UIView alloc]init];
searchview.frame=CGRectMake(0, 18,kScreen_Width-40 ,44);
// searchview.backgroundColor=[UIColor blueColor];
searchview.backgroundColor=[UIColor greenColor];
[self.view addSubview:searchview];
_searchBar = [[UISearchBar alloc] init];
searchview.layer.cornerRadius =20;
searchview.layer.masksToBounds = YES;
// _searchBar.backgroundColor = [UIColor greenColor];//背景颜色
// _searchBar.searchBarStyle = UISearchBarStyleMinimal;
_searchBar.searchBarStyle = UISearchBarStyleProminent;
_searchBar.searchBarStyle = UISearchBarStyleDefault;
_searchBar.delegate = self;
_searchBar.placeholder = @"请搜索感兴趣的人";
_searchBar.barTintColor =[UIColor greenColor];
_searchBar.layer.borderWidth = 0;
for (UIView *subview in [[_searchBar.subviews firstObject] subviews]) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
}
}//去掉背景边框
_searchBar.tintColor = [UIColor blackColor];//光标颜色
UITextField *searchField = [self.searchBar valueForKey:@"searchField"];
if (searchField) {
// 背景色
[searchField setBackgroundColor:[UIColor greenColor]];
// 设置字体颜色 & 占位符 (必须)
searchField.textColor = [UIColor redColor];
searchField.placeholder = @"placeholder";
// 根据@"_placeholderLabel.textColor" 找到placeholder的字体颜色
[searchField setValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"];

}

// [[_searchBar.subviews objectAtIndex:0]removeFromSuperview];
[searchview addSubview:_searchBar];
[_searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@5);
make.left.equalTo(@2);
make.height.equalTo(@34);
make.right.equalTo(@-2);
}];

self.navigationItem.titleView=searchview;

你可能感兴趣的:(搜索框)