Change the background of UISearchBar's textField

uisearchbar textfield background color swift 4
change uisearchbar background color swift
uisearchbar background color ios 13
uisearchbar appearance
customize uisearchbar swift
uisearchbar textfield background color swift 5
uisearchbar style
search bar color

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:

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

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

searchBarStyle, A search bar style that specifies the search bar's appearance. Availability. iOS 7.0 +; Mac Style { get set } The tint color to apply to the search bar background. Code snippet for changing UISearchBar placeholder color. Now with just a single line of code, you can change a UISearchBar 's placeholder color. searchBar.changePlaceholderColor (

In Swift 3:

if let txfSearchField = searchController.searchBar.value(forKey: "_searchField") as? UITextField {
        txfSearchField.borderStyle = .none
        txfSearchField.backgroundColor = .lightGray

Xcode 11 Beta 6 UISearchBar textfield background/tintcolor color, Any idea how to set the UISearchBar textfield's background color separately from the search bar's tintcolor? [UINavigationBar� I'm trying to change the background color of input field of UISearchBar.It's the rounded view where you input text to search, the default color is white. I would like to change it to gray I tried:

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;

Customize Color, background from UISearchBar in Swift � GitHub, Customize Color, background from UISearchBar in Swift PS: I have set clipToBounds to be true and everything I could think of and my searchbar is minimal. How to change inside background color of UISearchBar component on iOS. 20. Change the background of UISearchBar's textField. Related. 988. How to change the name of

Swift 5 and iOS 13

searchBar.searchTextField.backgroundColor = .white

You can direct change colour of textfield background.

How to set the UISearchBar text field background color, Just to be sure, try if you can set another property of the UISearchBar. I'm guessing that the connection to the IBOutlet is wrong, since you didn't make a property� How to change inside background color of UISearchBar component , Use this code to change the searchBar's UITextField backgroundImage: UITextField *searchField; NSUInteger numViews = [searchBar.subviews count]; for (int i Code snippet for changing UISearchBar placeholder color. Now with just a single line of code, you can change a UISearchBar's placeholder color. searchBar.changePlaceholderColor(

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

Customise the UISearchBar in Swift, Therefore we can access the UITextField through the key and perform necessary changes with it. Change Search Bar Background Color� I write it in the AppDelegate but I guess it works if you put it somewhere else too. You can do this by accessing the UITextField inside the searchBar, then you can change its background color, text color, and all other UITextField properties add the following extension to access the textField

Customize UISearchBar for different iOS versions, Changing Placeholder color, Changing Background color or change UITextField rightView the list is very long. I have wasted so many hours� button, then select Settings > Personalization to choose a picture worthy of gracing your desktop background, and to change the accent color for Start, the taskbar, and other items. The preview window gives you a sneak peek of your changes as you make them. In Background, select a picture or solid color, or create a slideshow of pictures.

iOS Notes 30: How to customize UISearchBar? [Updated v2], First set your new clear icon image programmatically and then go to the Assets folder and change your icon's rendering mode manually. let� Answers: The UISearchBar has a UITextField inside, but there’s no property to access it. So, there is no way for doing it in a standard way. But there is a non-stardard way to workaround it. UISearchBar inherits from UIView, you can access it’s subviews using [searchBar subviews].

How do you change the iOS navigation Searchbar background , Hi, I'm using the search bar that's built into the navigation bar (iOS 11+) and I'm trying to change the backgroud colour of the text box (not the� Changing Placeholder color, Changing Background color or change UITextField rightView the list is very long. Now with just single line of code you can change UISearchBar right view icon.