簡體   English   中英

為搜索欄設置取消按鈕文本顏色(Swift)

[英]Set cancel button text colour for search bar (Swift)

我試圖在Swift中設置搜索欄旁邊的取消按鈕的文本顏色。 這是我的代碼:

func searchDisplayControllerWillBeginSearch(controller: UISearchDisplayController) {
    self.searchDisplayController?.searchBar.showsCancelButton = true
    var cancelButton: UIButton
    var topView: UIView = self.searchDisplayController?.searchBar.subviews[0] as! UIView
    for subView in topView.subviews {
        if subView.isKindOfClass(NSClassFromString("UINavigationButton")) {
            cancelButton = subView as! UIButton
            cancelButton.setTitleColor(UIColorFromRGB(0x0096FF), forState: UIControlState.Selected)
            cancelButton.setTitleColor(UIColorFromRGB(0x0096FF), forState: UIControlState.Normal)
        }
    }
}

它適用於突出顯示的狀態,但不適用於正常狀態。 我知道在Objective-C中我可以使用appearanceWhenContainedIn但是在Swift中不存在。

有任何想法嗎?

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {

    searchBar.setShowsCancelButton(true, animated: true)
    for ob: UIView in ((searchBar.subviews[0] )).subviews {

        if let z = ob as? UIButton {
            let btn: UIButton = z
            btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
        }
    }
}

它是UISearchBarDelegate的委托方法:)

暫無
暫無

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

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