2016-11-17 20 views
0

我有下面這段代碼在我AppDelegate.swift自定義每一個的UISearchBar從AppDelegate中不工作

func customizeBars() { 
     let bartintColor = UIColor(colorLiteralRed: 20/255, green: 160/255, blue: 160/255, alpha: 1) 
     UISearchBar.appearance().tintColor = bartintColor 
     window!.tintColor = UIColor(red: 10/255, green: 80/255, blue: 80/255, alpha: 1) 
} 

我在didFinishLaunchingWithOptions方法調用這個函數(customizeBars()):

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     customizeBars() 
     return true 
} 

我也有這個在UISearchBArDelegate中是否有所作爲:

func position(for bar: UIBarPositioning) -> UIBarPosition { 
    return UIBarPosition.topAttached 
} 

但由於某種原因,當我運行我的應用程序時,它不會更改我的應用程序或我的window色調中的任何UISearchBarstintColor。有什麼我做錯了嗎?

enter image description here

+0

是否要更改文本字段文本顏色,取消按鈕和搜索圖標顏色的顏色? –

+0

@Saleh是的我想改變這些東西:Textfield文本顏色,取消按鈕,僅搜索圖標顏色,以及UISearchBar周圍的部分,就像我上面圖片中的灰色部分。如圖所示,唯一改變的是光標的顏色。 – CapturedTree

+0

你應該能夠創建UISearchBar的擴展,它在初始化時改變顏色。 –

回答

0

您可以使用此下面的代碼行更改搜索欄的顏色 -

UISearchBar.appearance().barTintColor = UIColor.red // Change the color here

但是我看到的是,色調的顏色(取消按鈕,搜索圖標和文本顏色)在iOS 10,swift 3中不起作用。 它以前在舊版本中工作。讓我試試如果我能找到某些東西來改變搜索欄的色調。

+0

我在'UISearchBar.appearance()'上看不到'barTintColor'屬性。我只看到'tintColor'。另外當我這樣做時,我實際上注意到顏色確實發生了變化,除了隱藏在'UISearchBar'所在的灰色部分之後。我應該刪除UISearchBar的背景視圖嗎? – CapturedTree

+0

barTintColor是UISearchbar的屬性,用於更改條形顏色,同樣的事情也發生在我身上,它不顯示在UIsearchbar建議框中。但是,您打開UISearchbar類,然後您會看到該功能可用。您可以刪除UISearchbar的背景視圖。 –

+0

好像你必須爲每個搜索欄手動更改其餘元素的顏色。無法通過使用appearance()函數來改變它。這肯定是來自蘋果的一個bug。 –

相關問題