2014-09-26 57 views
27

從Xcode 5升級到6,現在我的搜索欄色調爲黑色。無法更改搜索欄色彩顏色在iOS 8中透明

試圖改變它通過故事板右窗格>「酒吧色調」來清除顏色,但它仍然是黑色。

也試過編程:

[self.searchBar setTintColor:[UIColor clearColor]]; 

仍是黑色的:(

任何想法

+1

我想你想設置[barTintColor](https://developer.apple。COM /庫/ IOS /文檔/ UIKit的/參考/ UISearchBar_Class/index.html中#// apple_ref/OCC/instp /的UISearchBar/barTintColor)。 – CrimsonChris 2014-09-26 17:18:34

+0

你想讓搜索欄的*背景清晰,或者搜索欄的*文本框的背景清晰嗎? – Mike 2014-09-28 15:30:41

+0

搜索欄背景 - 橙色矩形 - 見下面 – 2014-09-29 09:29:47

回答

63

tintColor財產上的搜索欄,就像UINavigationBar的,改變了按鈕的顏色,以及改變閃爍光標的顏色,而不是實際的搜索欄背景,你想要使用的是barTintColor屬性。

searchbar.barTintColor = [UIColor orangeColor]; 
searchbar.tintColor = [UIColor greenColor]; 

產生如下的醜陋,但內容豐富,成果:

enter image description here

如果你想擁有一個完全透明的搜索欄,你需要設置背景圖片,以及:

searchbar.barTintColor = [UIColor clearColor]; 
searchbar.backgroundImage = [UIImage new]; 

enter image description here

編輯:我強烈建議不要穿越並修改任何 UIKit對象的子視圖,正如其他答案中所提出的。從蘋果公司的文檔:

對於UIKit中和其他系統框架中聲明覆雜的視圖,該視圖的任何 子視圖通常被認爲是專用的,並且在任何時候 變化。因此,您不應嘗試檢索或修改這些類型的系統提供的視圖的子視圖。如果這樣做, 您的代碼可能在未來的系統更新期間中斷。

https://developer.apple.com/documentation/uikit/uiview/1622614-subviews

+1

searchbar.barTintColor = [UIColor orangeColor];使它成爲橙色,但是當使用[UIColor clearColor]時,它仍然是黑色的,出於某種奇怪的原因 – 2014-09-26 17:40:33

+0

請參閱我的編輯,您需要更改背景視圖。 – Mike 2014-09-26 18:06:23

+2

對於那些仍然有這個問題,我實際上必須設置searchBar.scopeBarBackgroundImage = [UIImage new];以及。 – csundman 2014-10-24 21:50:11

0

我已經看到了這個問題,以及在iOS8上/ XCode的6.我沒有得到的搜索欄半透明。 searchbar.barTintColor或searchbar.tintColor設置清除顏色沒有幫助或顯示黑色的搜索欄。

我發現的唯一解決方法是設置半透明背景PNG圖像(alpha = 0.0)並設置searchbar.barTintColor以清除顏色。

7

我使用雨燕2.0這種方式來改變它在iOS 9:

let image = UIImage() 

    searchBar.setBackgroundImage(image, forBarPosition: .Any, barMetrics: .Default) 
    searchBar.scopeBarBackgroundImage = image 
+0

好的解決方案。需要注意的是,該解決方案只有在搜索條是半透明時纔可用。 – Kashif 2017-01-01 19:23:39

2

在斯威夫特3:

搜索欄的背景設置爲空圖像:

searchBar.setBackgroundImage(image, for: .any, barMetrics: .default) 
    searchBar.scopeBarBackgroundImage = image