鍍金池/ 問答/C  iOS/ ios11 下 導(dǎo)航欄下的搜索欄字體顏色怎樣設(shè)置?

ios11 下 導(dǎo)航欄下的搜索欄字體顏色怎樣設(shè)置?

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;

2017年8月19日 11:34
編輯回答
蝶戀花

試試這個 我是這么寫的 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;
        }
    }
}
2018年3月17日 19:30
編輯回答
安若晴
 [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{NSForegroundColorAttributeName: [UIColor greenColor]}];
    [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"Search" attributes:@{NSForegroundColorAttributeName: [UIColor orangeColor]}]];
2018年2月16日 14:56