iOS 11 UISearchBar background color

混江龙づ霸主 提交于 2019-12-18 20:05:08

问题


I understand that this question has been asked many, many times on SO. However, as Apple does best, with the release of iOS 11, they seem to have made a seemingly unnecessary change to the UISearchBar, specifically it's view hierarchy.

In further, the "text field" of a search bar is no longer accessible in the search bar's subviews, causing all of the previous solutions to "access" and change the background color of the text field, or any property of the text field for that matter.

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

FYI: I am specifically talking about the color behind the text... which now as of 11 defaults to white unless you specify the search bar style to be minimal.

UPDATE 1:

Since my posting of this question, I still have not found a valid or really any real solution to this issue. The closest I have seem to come is to dive deep into the appearance for instance properties

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

of the UISearchBar. Playing around with the found UITextField via methods such as the following:

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 begin drawing a new background view to be placed behind the view. However, the issues I had found too tedious to pursue further were drawing the a view with the correct frame / bounds to mimic exactly the original background.

Hopefully someone can find the actual solution to this problem. Nice miss apple...


回答1:


I think you may be looking for this, right? But I've it 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 result:




回答2:


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




回答3:


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 Views Hierarchy

If we set background colors for UISearchBar with code above, we'll see the colored subviews as follow images(click links to see). 

backgroundColor for Superview of UISearchBar subviews

We can see the Class Name of green view is UISearchBar in Object inspector.

So, if we use searchBar.backgroundColor = .green, we'll set the backgroundColor of Superview green. Therefore, the UISearchBar instance property backgroundColor will set the superview's background color.

Superview of UISearchBar

barTintColor for UISearchBarBackground

We can see the Class Name of red view is UISearchBarBackground in Object inspector.

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

If we use searchBar.barTintColor = .red, we'll set the backgroundColor of UISearchBarBackground's view red. In order to remove two black border on the tint bar layer, we have to remove the background from superview.

barTintColor of UISearchBar

searchField?.backgroundColor for UITextField

We can see the Class Name of yellow view is _UISearchBarSearchFieldBackgroundView (Subview of UISearchBarTextField) in Object inspector.

There's no direct method to access the searchField, same as searchBarBackground. We can also use KVC searchField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField try to get searchField.

If we use searchField?.backgroundColor = .yellow, we'll set the backgroundColor of UITextField yellow. Therefore, if we want to set text field background color, we have to access the searchField with KVC first

UITextField of UISearchBar




回答4:


Swift 4-5

searchController.searchBar.barTintColor = .white



来源:https://stackoverflow.com/questions/46436058/ios-11-uisearchbar-background-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!