3

I use this in an earlier version to get the textfield in my searchBar...

UITextField *searchField = [self.navigationItem.searchController.searchBar valueForKey:@"_searchField"];

In iOS11, it's still worked, I can change the text、font、even tintcolor,but I just can't set the textcolor, please teach me.

UISearchController *mySearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
mySearchController.searchResultsUpdater = self;
self.navigationItem.searchController = mySearchController;

// mySearchController.hidesNavigationBarDuringPresentation = false; self.navigationItem.hidesSearchBarWhenScrolling = NO;

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];

//this code is no use
txfSearchField.textColor=[UIColor yellowColor];

txfSearchField.backgroundColor=[UIColor whiteColor];
UIView *backgroundview= [[txfSearchField subviews]firstObject ];
backgroundview.backgroundColor=[UIColor whiteColor];
backgroundview.layer.cornerRadius = 8;
backgroundview.clipsToBounds = true;
Muhammad Umair
  • 1,664
  • 2
  • 20
  • 28
tony
  • 109
  • 1
  • 8
  • 2
    Possible duplicate of [UISearchBar text color change in iOS 7](https://stackoverflow.com/questions/19048766/uisearchbar-text-color-change-in-ios-7) – brandonscript Oct 27 '17 at 04:32
  • @brandonscript NO, the satuation is different – tony Oct 27 '17 at 06:17
  • UISearchController _searchField is private API, so you can't use it safely. Maybe changing color does'nt work since iOS 11 because the textField has an attributedText set. Try to replace txfSearchField.attributedText. – vmeyer Oct 27 '17 at 07:49
  • @vmeyer Thanks for answering, but stil no use.I can change the text this way ,but can't change the textcolor. – tony Oct 27 '17 at 08:07

3 Answers3

7

I finally find the way, write like this:

[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{NSForegroundColorAttributeName: [UIColor greenColor]}];
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"Search" attributes:@{NSForegroundColorAttributeName: [UIColor orangeColor]}]];
tony
  • 109
  • 1
  • 8
6

In swift, you can try:

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.red]

I find this answer here: iOS 11 customise search bar in navigation bar

Also tried direct set defaultTextAttributes of txfSearchField to [NSAttributedStringKey.foregroundColor.rawValue: UIColor.red] will not change the text color. But change backgroundColor will work, do not know why.

Pranjal Bikash Das
  • 1,092
  • 9
  • 27
yzyanchao
  • 105
  • 6
2

Tony's answer works great. Here is the Swift 5-Version for these lines:

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green]
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "Search", attributes: [NSAttributedString.Key.foregroundColor: UIColor.orange])
Teetz
  • 3,475
  • 3
  • 21
  • 34