前段時間嘗試了UISearchController轴总,自帶的動畫效果非常好沟绪。微信的搜索目前就是用這個怔檩。
但是項目的SearchBar用在導(dǎo)航欄褪秀,UISearchController不太適用了。
UISearchBar *searchBar = [[UISearchBar alloc]init];
_searchBar = searchBar;
[searchBar setPlaceholder:@" 搜索醫(yī)院名稱"];
_searchBar.delegate = self;
[_searchBar setBackgroundColor:[UIColor whiteColor]];
[searchBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]]];
//調(diào)整搜索圖片向右偏移8
[searchBar setPositionAdjustment:UIOffsetMake(8, 0) forSearchBarIcon:UISearchBarIconSearch];
//獲取textField(也可以通過KVC獲取)
UITextField *searchField=[((UIView *)[searchBar.subviews objectAtIndex:0]).subviews lastObject];
//設(shè)置placeHolder字體的顏色
[searchField setValue:[UIColor colorWithString:@"#CCCCCC"]forKeyPath:@"_placeholderLabel.textColor"];
searchField.layer.cornerRadius = 14;
searchField.layer.masksToBounds = YES;
searchField.layer.borderWidth = 0.8;
searchField.layer.borderColor = [[UIColor colorWithString:@"#D7D7D7"] CGColor];
searchField.backgroundColor = [UIColor whiteColor];
searchBar.frame = CGRectMake(0, 0, SCREENW, 44);