Ios 如何更改'的颜色;取消';Swift中UISearchBar上的按钮

Ios 如何更改'的颜色;取消';Swift中UISearchBar上的按钮,ios,swift,uisearchbar,Ios,Swift,Uisearchbar,我已在我的PFQueryTableViewController顶部添加了一个ui搜索栏。我已将搜索栏的颜色更改为我的应用程序的颜色,但在执行此操作时,它似乎也将其右侧的“取消”按钮的颜色更改为相同的颜色。理想情况下,我希望颜色是白色 此图显示了它当前的外观: 看起来没有“取消”按钮,但有,它的颜色与搜索栏的颜色相同(您仍然可以按它等) 有没有办法把“取消”按钮的颜色改成白色?我所做的一切似乎都没有什么不同 我用来制作ui搜索栏的代码此颜色为: UISearchBar.appearance().

我已在我的
PFQueryTableViewController
顶部添加了一个
ui搜索栏。我已将搜索栏的颜色更改为我的应用程序的颜色,但在执行此操作时,它似乎也将其右侧的“取消”按钮的颜色更改为相同的颜色。理想情况下,我希望颜色是白色

此图显示了它当前的外观:

看起来没有“取消”按钮,但有,它的颜色与搜索栏的颜色相同(您仍然可以按它等)

有没有办法把“取消”按钮的颜色改成白色?我所做的一切似乎都没有什么不同

我用来制作ui搜索栏的代码此颜色为:

UISearchBar.appearance().barTintColor = UIColor(hue: 359/360, saturation: 67/100, brightness: 71/100, alpha: 1)
UISearchBar.appearance().tintColor = UIColor(hue: 359/360, saturation: 67/100, brightness: 71/100, alpha: 1)
在故事板中,我设置了以下内容:

最后,为了使占位符和搜索栏中的文本变为白色,我使用了:

for subView in self.filmSearchBar.subviews {

    for subsubView in subView.subviews {

        if let searchBarTextField = subsubView as? UITextField {

            searchBarTextField.attributedPlaceholder = NSAttributedString(string: NSLocalizedString("Search Cinevu film reviews", comment: ""), attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])

            searchBarTextField.textColor = UIColor.whiteColor()

        }

    }

}
谢谢你的帮助!:)

你试过了吗
UISearchBar.appearance().tintColor=UIColor.whiteColor()

环顾四周,这似乎是实现我所需的最佳方法:

 let cancelButtonAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
 UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes , for: .normal)

基于Nick89的代码,我对Swift 3.1做了这样的更改

let cancelButtonAttributes: [String: AnyObject] = [NSForegroundColorAttributeName: UIColor.white]

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)

我只想更改
UISearchBar
而不是所有UIBarButton,因此我使用like
UIBarButtonItem.appearance(当包含[UISearchBar.self]实例时)
使用代码中的这一行更改取消按钮的颜色:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.white], for: .normal)

使用Swift 4.0查看Xcode 9.2,我知道这个问题有足够的答案,但这里有一个简单的实现方法,搜索栏背景颜色和取消按钮背景颜色,我们可以在情节提要属性检查器中直接更改它

搜索栏背景色

搜索栏取消按钮颜色


Swift 4.2版本,基于其他答案:

let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.orange]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)

借助Swift 4.0版本2017-09-19工具链,这起到了以下作用:

    let cancelButtonAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)

Swift
4.2、4.1

添加的自定义类可用于自定义搜索栏中最常见的元素。自定义类
SearchBar
会导致出现以下搜索栏


Swift 4.2

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)

以上所有答案对我都不适用。(获取'Type'NSAttributedString.Key'(也称为'NSString')没有成员'foregroundColor'错误)

也许因为我在斯威夫特3

下面是对我有用的稍加修改的代码:-

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSForegroundColorAttributeName : .black], for: .normal)
注:-

如果您使用的是UISearchController,请将此代码插入“willPresentSearchController:”或“didPresentSearchController:”

您只需设置搜索栏的tintcolor即可更改取消按钮的颜色。这是因为斯威夫特4,所以应该做的把戏

let searchBar = UISearchBar(frame: )
searchBar.tintColor = .orange

Swift 5

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([.foregroundColor : UIColor.barTint], for: .normal)

“取消”按钮以前是什么颜色?@cbay它是默认颜色,像浅灰色/浅灰色我没有使用过搜索栏(我以编程方式处理所有UI内容),但是“取消”按钮的代码在哪里?你应该可以用cancelButton.backgroundColor=UIColor.greyColor()之类的东西来更改颜色我刚才在看文档。ShowScanCell按钮默认为否。请尝试将其设置为是。如果可能的话,我找不到在哪里换颜色。可能对你有帮助。我有。是的,它不会改变文本的颜色。它只是在按钮区域添加了一个模糊的轮廓。你有一个可以上传的示例应用程序吗?@DavidYangLiu它还会更改光标颜色?如果你使用UISearchController,你必须将此代码插入willPresentSearchController:或didPresentSearchController:这会影响应用程序中的其他内容吗?还是只影响取消按钮?@ArthurGarza It影响非法胜利的取消按钮。Xcode 9.4.1、Swift 4.0、iOS 11.4.1只是为了简化一点:。。。setTitleTextAttributes([.foregroundColor:UIColor.white],for:.normal)与Swift 4中的答案相同:
让cancelButtonAttributes:[NSAttributedString.Key:Any]=[NSAttributedString.Key.foregroundColor:UIColor.gray]UIBarButtonItem.appearance(包含实例时:[UISearchBar.self])。setTitleTextAttributes(cancelButtonAttributes,用于:。正常)