簡體   English   中英

無法在 iOS 8 中將搜索欄色調顏色更改為透明

[英]Can't change search bar tint color to be transparent in iOS 8

從 Xcode 5 升級到 6,現在我的搜索欄色調是黑色的。

試圖通過故事板右窗格>“Bar Tint”更改它以清除顏色,但它仍然是黑色。

還以編程方式嘗試過:

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

還是黑的:(

有任何想法嗎?

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

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

產生以下丑陋但信息豐富的結果:

在此處輸入圖片說明

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

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

在此處輸入圖片說明

編輯:我強烈建議不要像其他答案中提出的那樣遍歷和修改任何UIKit 對象的子視圖。 來自蘋果的文檔:

對於 UIKit 和其他系統框架中聲明的復雜視圖,視圖的任何子視圖通常都被視為私有的,並且隨時可能更改。 因此,您不應嘗試檢索或修改這些類型的系統提供的視圖的子視圖。 如果這樣做,您的代碼可能會在未來的系統更新期間中斷。

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

我必須以這種方式使用 Swift 2.0 在 iOS 9 上更改它:

    let image = UIImage()

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

在 Swift 3 上:

將 searchBar 的背景設置為空圖像:

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

以編程方式更改搜索欄色調顏色:

    if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
        textfield.textColor = #colorLiteral(red: 0.3921568627, green: 0.3921568627, blue: 0.3921568627, alpha: 1)
        textfield.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
    }

對於故事板:

在此處輸入圖片說明

我在 iOS8/XCode 6 上也看到過這個問題。我沒有讓搜索欄變成半透明。 searchbar.barTintColor 或 searchbar.tintColor 設置清除顏色沒有幫助或顯示黑色搜索欄。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM