Change the background of UISearchBar's textField
Asked Answered
G

7

21

I'm struggling with this for a while now. Have searched everywhere but the solution provided only works with objective-c. Which consists in

UITextField *txt = [_searchBar valueForKey:@"_searchField"];

Although some might say that this is protected API and Apple might refuse an app with such code.

So, now I've tried so many ways to work this out and it's not working at all. My code is this:

searchBar = UISearchBar(frame: CGRectMake(0, 0, 320.0, 30.0))
searchBar.autoresizingMask = UIViewAutoresizing.FlexibleWidth
searchBar.searchBarStyle = UISearchBarStyle.Minimal
searchBar.layer.cornerRadius = 15
searchBar.delegate = self

searchBar.backgroundColor = UIColor.whiteColor()

if floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1{
    // Load resources for iOS 6.1 or earlier
    searchBar.placeholder = "Search";
} else {
    // The following "hack" (if we can call that) breaks the UI for different size devices.
    // Load resources for iOS 7 or later
    searchBar.placeholder = "Search                                         ";
}

This gives me the bizarre result: enter image description here

While what I want is just the textfield inside the UISearchBar to have a white background and the SearchBar itself have a cornerRadius of, say, 15.

How can I do this?

Thanks so much in advance

Grimbly answered 20/1, 2015 at 22:45 Comment(1)
UITextField *searchField = [SearchBar valueForKey:@"_searchField"]; searchField.textColor = [UIColor whiteColor]; I have already used this code in my app and app it already available in App Store..Madden
A
32

You have to access the UITextField inside the UISearchBar. You can do that by using valueForKey("searchField")

var textFieldInsideSearchBar = yourSearchbar.valueForKey("searchField") as? UITextField

textFieldInsideSearchBar?.textColor = yourcolor
textFieldInsideSearchBar?.backgroundColor = backgroundColor
...

there you can change any parameters like textColor or backgroundColor of the UITextField

Amorino answered 2/8, 2015 at 15:34 Comment(2)
you should set style as Prominent or Default. If you prefer to use Minimal, backgroundColor is not working. searchBar.searchBarStyle = UISearchBarStyle.Prominent // or .DefaultBubal
@Bubal thanks this is indeed really important to NOT use Minimal style! spent too much time on this....Randolphrandom
B
18

In Swift 3:

if let txfSearchField = searchController.searchBar.value(forKey: "_searchField") as? UITextField {
        txfSearchField.borderStyle = .none
        txfSearchField.backgroundColor = .lightGray
    }
Beefy answered 4/6, 2017 at 19:8 Comment(0)
P
10

Adding to the above answer. If you want to keep the search bar style UISearchBarStyleMinimal and change the background of UISearchBar's textField, set the textField's borderStyle to UITextBorderStyleNone

Objective C:

self.searchBar.searchBarStyle = UISearchBarStyleMinimal;

UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
    txfSearchField.borderStyle = UITextBorderStyleNone;
    txfSearchField.backgroundColor = yourColor;
Psalms answered 6/3, 2017 at 13:19 Comment(2)
I have searched around. This is the solution that really works !Magnificat
I concur. This solution works when UISearchBarStyle is set to .minimal. Tested on iOS 10.3Latoyialatreece
E
9

Swift 5 and iOS 13

searchBar.searchTextField.backgroundColor = .white

You can direct change colour of textfield background.

Elconin answered 29/11, 2019 at 11:11 Comment(1)
Warning: searchTextField is only available after iOS 13.Doenitz
F
3

Add an extension to UISearchBar to access its UITextField:

import UIKit

extension UISearchBar {

    var textField: UITextField? {
        let subViews = subviews.flatMap { $0.subviews }
        return (subViews.filter { $0 is UITextField }).first as? UITextField
    }
}

Then you can set its properties as you'd expect:

searchController.searchBar.textField?.backgroundColor = .red
searchController.searchBar.textField?.tintColor = .yellow
Fonteyn answered 9/4, 2019 at 15:21 Comment(4)
You shouldn't answer questions when you haven't tried the answer. This doesn't work in iOS 12 for instance.Coronel
@ClausJørgensen could you elaborate on what is "not working"?Fonteyn
searchController.searchBar.textField? is always nil in my caseAviation
Besides we can now do serachBar.searchTextField.backgroundColor = .white so this answer may have been valid for old SDK's but not in recent SDK'sAviation
M
1

searchController.searchBar.searchTextField.backgroundColor = UIColor.white searchController.searchBar.searchTextField.textColor = UIColor.black

Myronmyrrh answered 9/12, 2019 at 12:57 Comment(0)
B
0

When using iOS 13 or newer, the searchTextField property can be directly accessed.

However, setting the backgroundColor only works as expected when the borderStyle is set to .none. This results in a text field without rounded borders. To fix this, use this code:

searchController.searchBar.searchTextField.borderStyle = .none
searchController.searchBar.searchTextField.backgroundColor = .white // Or any other UIColor
searchController.searchBar.searchTextField.layer.cornerRadius = 8.0
Bokbokhara answered 1/8, 2023 at 22:42 Comment(2)
The searchTextField property has been available on UISearchBar since iOS 13.0Tuscany
@Tuscany You're right!Bokbokhara

© 2022 - 2024 — McMap. All rights reserved.