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

吃可爱长大的小学妹 提交于 2019-12-10 13:50:10

问题


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:

To something like this


回答1:


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.




回答2:


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



回答3:


Dont think you can, tried it myself and it seems to be part of the API, heres a similar post that asks the same thing The reason the FB one is a solid colour is because afaik the FB app use the Three20 framework. But even with all their scaling down its still a raw 30 megs in size, extreme overkill for a search bar in your case

Dont even know if an image will work as you can't set the area around the bar itself to be clear or transparent to put an image under it



来源:https://stackoverflow.com/questions/6696200/ios-how-to-replace-search-bar-background-shine-glow-with-one-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!