How to change background color of UiSearchBar to black -SWIFT

10,223

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
Share:
10,223
Anish Parajuli 웃
Author by

Anish Parajuli 웃

Data Science Student at RMIT University

Updated on June 14, 2022

Comments

  • Anish Parajuli 웃
    Anish Parajuli 웃 almost 2 years

    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

  • Nick89
    Nick89 over 8 years
    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) ? thanks
  • dip
    dip over 8 years
    till now no i guess, for that we have to create custom search bar !