Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: How to replace Search Bar background shine/glow with one color

How do I remove the shine/glow (gradient) from the background of a Search Bar. I would like to replace it with just one color.

From this:

enter image description here

To something like this

enter image description here

like image 552
Nikolaj Avatar asked Nov 05 '22 18:11

Nikolaj


2 Answers

Use

searchbar.backgroundImage = //your image here

and you can use a nifty category that I created for uiimage that will generate an image based on a color

https://github.com/Hackmodford/HMUIImage-imageWithColor

So all together it will look like this

searchBar.backgroundImage = [UIImage imageWithColor:[UIColor blueColor]];

That will generate the effect you want.

like image 76
Hackmodford Avatar answered Nov 11 '22 05:11

Hackmodford


1 To remove the glossy background:

UISearchBar has a subview of class UISearchBarBackground, just cycle through the subviews and when you find a subview of that class, set its alpha to 0.

RubyMotion example:

searchBar.subviews.each { |v| v.alpha = 0 if v.class == UISearchBarBackground }  

2 To add a solid background override the drawRect method:

RubyMotion example:

def drawRect(rect)
  blackColor = '#222222'.to_color
  context = UIGraphicsGetCurrentContext()
  CGContextSetFillColor(context, CGColorGetComponents(blackColor.CGColor))
  CGContextFillRect(context, rect)      
end
like image 21
defvol Avatar answered Nov 11 '22 04:11

defvol