更改色调颜色不会更改搜索栏内“取消”按钮的字体

时间:2016-03-15 19:15:28

标签: ios swift uisearchcontroller

我有一个名为SearchView的UIView,我将searchController.searchBar添加为UIView的子视图。

在viewDidLoad()中,我正在更改我的UISearchController中的搜索栏,如下所示:

    //makes background transparent
    searchController.searchBar.backgroundImage = UIImage()

    //makes magnifying glass white
    let iconView:UIImageView = tf!.leftView as! UIImageView
    iconView.image = iconView.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
    iconView.tintColor = UIColor.whiteColor()

    //changes text color to white
    let tf = searchController.searchBar.valueForKey("searchField") as? UITextField
    let attributedString = NSAttributedString(string: "", attributes:[NSForegroundColorAttributeName : UIColor.whiteColor()]) 
    tf!.attributedPlaceholder = attributedString
    tf!.textColor = UIColor.whiteColor()
    //changes search field background color
    tf!.backgroundColor = UIColor(red: 82/255, green: 91/255, blue: 93/255, alpha: 1) 

    //HERE: should make the cancel button font white...
    searchController.searchBar.tintColor = UIColor.whiteColor()

    searchView.addSubview(searchController.searchBar)

在AppDelegate中,我有

UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).tintColor = UIColor.whiteColor()

1 个答案:

答案 0 :(得分:1)

在发布时的应用代表中,请说:

UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).tintColor = UIColor.whiteColor()

请注意,只有在搜索栏中进行编辑时才会生效。如果搜索栏中没有编辑,则没有任何内容可以取消,因此“取消”按钮变暗并且没有颜色(这是一种基于色调颜色的灰色)。

enter image description here

相关问题