1. 程式人生 > >iOS修改UISearchBar背景顏色

iOS修改UISearchBar背景顏色

貌似很簡單的事啊,直接用:

searchBar.backgroundColor = .red

不就OK了???

其實,不可以!!!

因為實際上UISearchBar的層次關係是這樣的:

這裡寫圖片描述

這裡寫圖片描述

如果按照上面那樣設定的將是UISearchBar自身的背景色,但是因為它上面還覆蓋了UIView和UISearchBarBarBackground兩個檢視,所以你基本看不到效果(因為它們有略微透明,所以可以隱約看到一丟丟效果…)

所以正確的做法是想辦法將UISearchBarBarBackground去掉,它是一個UIImageView物件:

///設定搜尋條背景顏色
func setSearchBarBackgroundColor(to color:UIColor){
    guard searchBar != nil else
{return} if let subView = searchBar.subviews.first{ if let view = subView.subviews.first,let imgView = view as? UIImageView{ imgView.alpha = 0.0 searchBar.backgroundColor = color } } }

相關推薦

iOS修改UISearchBar背景顏色

貌似很簡單的事啊,直接用: searchBar.backgroundColor = .red 不就OK了??? 其實,不可以!!! 因為實際上UISearchBar的層次關係是這樣的: 如果按照上面那樣設定的將是UISearchBar自身的

IOS開發—無需自定義,修改tabbar背景顏色

UIView *backView = [[UIViewalloc] initWithFrame:CGRectMake(0, 0, kDeviceW, 49)];     backView.backgroundColor = kAppColorLightGray;     [

IOS 修改UISearchBar 輸入框的顏色 placeholder字型的顏色

UISearchBar *searchBar = [UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 100, 30)]; for (UIView* subview in [[searchBar.subviews la

PDF編輯工具怎麼修改PDF背景顏色

PDF檔案在工作中,甚至是生活中都會使用的到,而PDF檔案也不與其他的檔案一樣,PDF檔案的修改編輯是需要使用到PDF編輯工具的,那麼,PDF編輯工具怎麼修改PDF背景顏色呢,估計有很多的小夥伴都不知道應該怎麼做吧,那就一起看看下面的文章瞭解一下吧。 1.開啟執行迅捷PDF編輯器http://b

Android開發:通過樣式修改SeekBar背景顏色,進度條顏色,滑塊圖片

通過樣式style修改 seekBar 的 背景、進度、遊標等圖片。 佈局檔案: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.and

css輸入框placeholder樣式修改以及背景顏色漸變設定

程式碼如下 /* placeholder 樣式設定 */ input::-webkit-input-placeholder,textarea::-webkit-input-placeholder { font-size: 0.16rem; color: #999; font-style:

點選修改元素背景顏色

<style> .selected{ color: #FFFFFF; background: url(selected.png) no-repeat; background-size: 100% 100%; } </

修改TabControl背景顏色

修改tabControl的DrawMode屬性為OwnerDrawFixed,然後在tabControl的DrawItem事件中寫程式碼TabControl tc=sender as TabControl;Font font=new Font("微軟雅黑",10F);Soli

iOS 設定UITabBar,背景顏色,預設圖片,文字顏色,和選中顏色.

  對於UITabBar設定背景顏色很簡單,backbackgroundColor不要使用,即使設定了也不起作用,應該使用barTintColor設定背景.    那麼圖示和文字的預設顏色怎麼設定呢

如何修改MyEclipse背景顏色

編輯視窗右鍵單擊——》Preferences——》General加號——》Editors加號——》點Text Editors字樣——》右下視窗選Backgroud color,右邊點掉System Default按鈕,點Color右邊的顏色框,選擇顏色OK背景顏色向你推薦:R

iOS設定tabBar背景顏色

viewDidLoad中設定//設定tabBar的顏色    UIView *bgView = [[UIViewalloc] initWithFrame:self.tabBar.bounds];    bgView.backgroundColor = [UIColorblue

修改eclipse背景顏色

主要通過外掛來實現。 1. Eclipse Color Scheme http://eclipsecolorthemes.org/ 可以在Eclipse的Market裡面搜尋關鍵詞來安裝,安裝完畢之後,裡面附帶了最流行的theme。如果需要安裝其他theme,可以去

jquery入門 修改網頁背景顏色

我們在瀏覽一些網站,尤其是一些小說網站的時候,都會有修改頁面背景顏色的地方,這個功能使用jquery很容易實現。 效果圖: show you code: <!doctype html> <html> <head> <meta ch

IOS開發之修改UISearchBar背景顏色

mySearchBar.backgroundColor = RGBACOLOR(249,249,249,1);     mySearchBar.backgroundImage = [self imageWithColor:[UIColor clearColor] size:mySearchBar.bound

iOS UISearchBar背景顏色修改

測試環境 :iOS 9.3 修改內部輸入框的顏色為紅色 ,背景顏色為綠色 UISearchBar * search = [[UISearchBar alloc]initWithFrame:CGRe

UISearchBar 修改背景顏色透明

輸入相關關鍵字在百度中能搜尋出來很多,但是都不靠譜,於是Google了一下,在StackOverFlow中找到一個蠻靠譜的方法,試了可行。 with iOS8 sdks apple moved @"UISearchBarBackground" view one lev

修改IOSUISearchBar的取消按鈕背景、搜尋內容輸入文字框背景UISearchBar背景

 轉載請標明出處:http://blog.csdn.net/android_ls/article/details/39993433 測試的手機IOS系統版本號為:6.1.3,實現步驟如下: 1、新增U

iOS中如何修改UISegmentedControl的背景顏色和字型的顏色UISegmentedControl

segmentedControlStyle在iOS7 之後設定沒有效果了 那麼如何快速的設定 UISegmentedControl的背景顏色呢? #define RGBACOLOR(r, g, b, a)   [UIColor colorWithRed:(r)/255

iOS 修改狀態欄字體的顏色

light ani 字體 stat porting ios status usb 改變 在實際開發中,狀態欄有時,需要我們自己設置; 比如: 默認狀態欄 假如我們開發的view是黑色的,那麽效果如圖: 狀態欄是白底黑字,下面的view是黑底? 這樣子真的好嗎?說

iOS - 設置導航欄之標題欄居中、標題欄的背景顏色

nor right rect set line cati int tail 自定義 本章實現效果: Untitled.gif 前言: 項目中很多需求是要求自定義標題欄居中的,本人最近就遇到這中需求,如果用系統自帶的titleView設置的話,不會居中,經過嘗試,