UISearchController输入框颜色修改
2017-04-28 本文已影响94人
FlowYourHeart
- 查了一些资料,但一直没成功,也不知道是什么原因,取消按钮的颜色还没达到随意设置。
如题输入框颜色修改,代码:
_searchDC = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchDC.searchBar.frame = CGRectMake(0, 0, WIDTH, 44);//
self.searchDC.searchResultsUpdater = self;
[self.searchDC.searchBar sizeToFit];
self.searchDC.searchBar.barTintColor = [UIColor whiteColor];
UIButton *canceLBtn = [self.searchDC.searchBar valueForKey:@"cancelButton"];
[canceLBtn setTitle:@"取消" forState:UIControlStateNormal];
for (UIView *subView in [[self.searchDC.searchBar.subviews lastObject] subviews]){
if ([subView isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)subView;
textField.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
//修改输入的字体的颜色
textField.textColor = [UIColor redColor];
//修改placeholder的颜色
[textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
}
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *btn = (UIButton *)subView;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}
self.searchDC.searchBar.placeholder = @"请输入关键词";
self.searchDC.delegate = self;
self.searchDC.dimsBackgroundDuringPresentation = NO; // default is YES
self.searchDC.searchBar.delegate = self; // so we can monitor text changes + others
self.definesPresentationContext = YES; // know where you want UISearchController to be displayed
self.searchDC.hidesNavigationBarDuringPresentation = NO;//搜索时,Navigation不隐藏
取消按钮:目前在网上找的方法还没有成功的,有知道的朋友,请不吝相告。