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

dip
  • 3,548
  • 3
  • 24
  • 36
LC 웃
  • 18,888
  • 9
  • 57
  • 72
  • It may help you: http://stackoverflow.com/questions/13817330/how-to-change-inside-background-color-of-uisearchbar-component-on-ios. – Lydia Mar 26 '15 at 09:04
  • take a look please at my previous answer https://stackoverflow.com/a/45555462/3472073 – ale_stro Aug 07 '17 at 20:49

1 Answers1

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
dip
  • 3,548
  • 3
  • 24
  • 36
  • 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 – Nick89 Nov 24 '15 at 22:24
  • till now no i guess, for that we have to create custom search bar ! – dip Nov 25 '15 at 09:10