2017-08-14 96 views
0

UISearchbar add navigationController?.navigationBar。如何更改「取消」顏色UISearchbar add navigationController?.navigationBar,如何更改「取消」顏色

_searchBar = UISearchBar(frame: CGRect(frame: CGRect(x: 0, y: 0, width: 414, height: 64))) 
_searchBar!.delegate = self 
_searchBar!.showsCancelButton = true 
backgroundColor = .white 
barTintColor = .white 
tintColor = .red 
backgroundImage = UIImage() 
_searchBar!.placeholder = "搜索文件" 
navigationController?.navigationBar.addSubview(_searchBar!) 

此結果是「取消」顏色爲白色。我如何將它設置爲紅色?

+0

看到這個曾經https://stackoverflow.com/questions/35302760/如何改變取消按鈕上的uisearchbar-in-swift –

回答

0

使用appearance功能UIAppearance模塊 -

方法1: -可見取消按鈕的顏色,當searchBar上負載

let attributes = [NSForegroundColorAttributeName : UIColor.red] 
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal) 

或 -

方法2: - 可見取消按鈕顏色searchBar點擊後 -

UIBarButtonItem.appearance(whenContainedInInstancesOf:[UISearchBar.self]).tintColor = UIColor.red 
+0

@ Jack非常感謝你,這是工作,但它必須iOS9.0,​​@ Anbu.Karthink答案適用於t o所有系統版本 –

0

cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: ``UIColor.YOUR COLOR] UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as?[String : AnyObject], forState: UIControlState.Normal)

+0

在你的地方你的顏色ADD UIColor.YOUR COLOR –

0

改變了格式可讀性

let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: UIColor.YOUR COLOR]  UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], forState: UIControlState.Normal) 
相關問題