How to change background color of UiSearchBar to black -SWIFT
Asked Answered
K

2

6

I created UISearchBar programmatically as below and the button appears to be of second image.How to clear the white color of search bar and set background colour to Black

var categorySearchBar : UISearchBar = UISearchBar()
        categorySearchBar.frame = CGRectMake(0, 20, self.view.frame.size.width, 30)
        categorySearchBar.layer.borderColor = UIColor.grayColor().CGColor
        categorySearchBar.layer.borderWidth = 1.0
        categorySearchBar.placeholder = "SEARCH"
        categorySearchBar.barTintColor = UIColor.blackColor()
        self.view.addSubview(categorySearchBar)

figure 1

figure 2

Kitchen answered 26/3, 2015 at 8:40 Comment(2)
It may help you: #13817830.Mckay
take a look please at my previous answer https://mcmap.net/q/376104/-cannot-change-search-bar-background-colorMothy
T
8

You can use barStyle property of UISearchBar , if you are using story board you can use Attribute Inspector to change bar style, or use following code segment. It might help mate :)

categorySearchBar.barStyle = UIBarStyle.Black
Trinitroglycerin answered 3/4, 2015 at 5:2 Comment(2)
Is there a way to change it to any other colour, say red? or a custom colour, such as: UIColor(red: 83/255, green: 165/255, blue: 198/255, alpha: 0.4) ? thanksSmoothie
till now no i guess, for that we have to create custom search bar !Trinitroglycerin
D
0

Just in case someone is still struggling to stick search bar to dark mode, Just add this

self.navigationItem.searchController?.searchBar.overrideUserInterfaceStyle
= .dark
Disclamation answered 20/9, 2024 at 4:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.