IOS 11 UISearchBar Background Color

I understand that this question has been asked many, many times on SO. However, since Apple is doing everything possible with the release of iOS 11 , they seem to have made an unnecessary change for the UISearchBar , in particular the presentation hierarchy.

In addition, the β€œtext box” of the search bar is no longer available in the subzones of the search bar, resulting in all previous solutions β€œaccessing” and changing the background color of the text box or any property of the text box.

  • Does anyone know how to actually adjust the background color of the search bar in iOS 11 ?

FYI: I specifically talk about the color behind the text ... which is now 11 by default equal to white unless you specify the search bar style as minimal.

UPDATE 1:

From my publication of this question, I still have not found a real or really no real solution . The closest thing that seems to me is to plunge deeply into the appearance, for example, properties

 [[UISearchBar class] appearanceWhenContainedInInstancesOfClasses:(nonnull NSArray<Class<UIAppearanceContainer>> *)] 

UISearchBar . Play with the found UITextField using methods such as:

 if ([view isKindOfClass:[UITextField class]]) { return (UITextField*)view; } UITextField *searchTextField; for (UIView *subview in view.subviews) { searchTextField = [self searchViewForTextFieldBg:subview]; if (searchTextField) { break; } } return searchTextField; 

You can start drawing a background view of a new one that will be placed behind the view. However, the problems, which I found too tedious to continue further, outlined the view with the correct frames to accurately simulate the original background.

Hope someone finds a real solution to this problem. Nice miss apple ...

+7
ios objective-c swift ios11 uisearchbar
source share
3 answers

I think you can look for it, right? But I have this in Swift :(

 @IBOutlet weak var sbSearchBar: UISearchBar! if let textfield = sbSearchBar.value(forKey: "searchField") as? UITextField { textfield.textColor = UIColor.blue textfield.backgroundColor = UIColor.yellow } 

Here is the result:

enter image description here

+2
source share

This code changes the background color of the text field.

Swift 4

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { //background color of text field UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .cyan } 

This is the result.

example used to change the background to blue

+2
source share
 let searchBar = UISearchBar(frame: CGRect()) let searchField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField let searchBarBackground: UIView? = searchBar.value(forKey: "background") as? UIView // searchBarBackground?.removeFromSuperview() if searchField != nil { var frame = searchField?.frame frame?.size.height = 30 searchField?.frame = frame! searchField?.backgroundColor = .yellow } searchBar.barTintColor = .red searchBar.delegate = self searchBar.backgroundColor = .green 

Runtime View Hierarchy

If we set the background colors for the UISearchBar with the code above, we will see the colored subtitles as shown below (click links to see). οΏΌ

backgroundColor to view UISearchBar routines

We can see that the class name of the green UISearchBar view in the Object Inspector .

So, if we use searchBar.backgroundColor = .green , we will set backgroundColor of Superview green. Thus, the property of the UISearchBar backgroundColor instance will set the background color of the supervisor.

UISearchBar Overview

barTintColor for UISearchBarBackground

We can see that the class name of the red view is UISearchBarBackground in the Object Inspector .

However, there is no direct method to access the view, can we use KVC searchBar.value(forKey: "background") as? UIView searchBar.value(forKey: "background") as? UIView to get searchBarBackground .

If we use searchBar.barTintColor = .red , we will set the backgroundColor background color to red for UISearchBarBackground. To remove the two black borders of the hue layer, we need to remove the background from the supervisor.

barTintColor UISearchBar

searchField?.backgroundColor for UITextField

We can see that the class name of the yellow view is _UISearchBarSearchFieldBackgroundView (Subview of UISearchBarTextField) in the object inspector.

There is no direct method to access searchField , nor is searchBarBackground. Can we also use KVC searchField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField searchField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField searchField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField to get searchField .

If we use searchField?.backgroundColor = .yellow , we will set backgroundColor UITextField to yellow. Therefore, if we want to set the background color in the text box, we need to access the KVC search box first

UITextField UISearchBar

0
source share

All Articles