11以前的系統(tǒng)用
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后默認(rèn)顯示的,但會導(dǎo)致滑動的時候搜索欄不會隱藏
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;
//大標(biāo)題模式
} 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];
不好使
我的在cell中的textField也出這個問題了,臨時用下面方法解決:
NSAttributedString *title = [[NSAttributedString alloc] initWithString:textField.text attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:TabFontSize],NSForegroundColorAttributeName : [UIColor whiteColor]}];
textField.attributedText = title;
試試這個 我是這么寫的 ios11下親測可用
- (void)_configSearchBarBGColor:(UIColor *)custColor textColor:(UIColor *)textColor imageName:(NSString *)imageName{
//自定義placeholder的文字顏色和大小
UISearchBar *searchBar = self.searchBar;
[searchBar setImage:[UIImage imageNamed:imageName] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
for (UIView *subview in ((UIView *)searchBar.subviews[0]).subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
UITextField *textField = (UITextField *)subview;
textField.textColor = [UIColor whiteColor];
textField.backgroundColor = custColor;
[textField setValue:textColor forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
break;
}
}
}
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{NSForegroundColorAttributeName: [UIColor greenColor]}];
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"Search" attributes:@{NSForegroundColorAttributeName: [UIColor orangeColor]}]];
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。