11以前的系统用
UITextField *searchField = [self.navigationItem.searchController.searchBar valueForKey:@"_searchField"];
就能得到search的textfield然后去修改textcolor。在iOS11下也能这样获取到textfield,甚至也能去修改类似tintcolor等一些属性。但是textcolor就没有办法去修改了,求教如何在iOS 11下去修改textcolor?
附代码
#import "ViewController.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UISearchBar *searchBar;
@property (strong, nonatomic) UISearchController *searchController;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.extendedLayoutIncludesOpaqueBars = YES;
if (@available(iOS 11, *)) {
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
[self.view addSubview:self.tableView];
}
UISearchController *mySearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
mySearchController.searchResultsUpdater = self;
self.searchController = mySearchController;
self.navigationItem.searchController = mySearchController;
// mySearchController.hidesNavigationBarDuringPresentation = false;
self.navigationItem.hidesSearchBarWhenScrolling = NO;
//这里是让搜索栏在load后默认显示的,但会导致滑动的时候搜索栏不会隐藏
self.tableView.refreshControl = [[UIRefreshControl alloc] init];
[self.tableView.refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
self.navigationItem.title = @"xxxx";
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"reload" style:UIBarButtonItemStylePlain target:self action:@selector(refresh)];
self.navigationItem.rightBarButtonItem = item;
UITextField *txfSearchField = [self.navigationItem.searchController.searchBar valueForKey:@"_searchField"];
txfSearchField.tintColor=[UIColor blueColor];
txfSearchField.textColor=[UIColor yellowColor];
txfSearchField.backgroundColor=[UIColor whiteColor];
[txfSearchField setNeedsLayout];
// UIView *backgroundview= [[txfSearchField subviews] firstObject ];
// backgroundview.backgroundColor=[UIColor yellowColor];
// // Rounded corner
// backgroundview.layer.cornerRadius = 8;
// backgroundview.clipsToBounds = true;
// [self configSearchBarBGColor:[UIColor blueColor] textColor:[UIColor yellowColor] imageName:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (@available(iOS 11.0, *)) {
self.navigationController.navigationBar.prefersLargeTitles = YES;
//大标题模式
} else {
// Fallback on earlier versions
}
[self performSelector:@selector(refresh) withObject:nil afterDelay:1];
}
- (void)refresh {
NSLog(@"xx");
[self.tableView reloadData];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.tableView.refreshControl endRefreshing];
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
}
cell.textLabel.text = @(indexPath.row).stringValue;
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0.01;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
#pragma mark - 其他的delegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
self.navigationItem.hidesSearchBarWhenScrolling = YES;
//所以就要在滑动的时候吧这个打开,让它在滑动时能隐藏
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
}
@end
其中txfSearchField.textColor=[UIColor yellowColor];
不好使