简体   繁体   English

UISearchBar 取消按钮颜色?

[英]UISearchBar cancel button color?

When I drop a UISearchBar into my view inside Interface Builder, and change its style to Black Opaque, the cancel button stays unfittingly blue / gray and doesn't become black.当我将 UISearchBar 放入 Interface Builder 中的视图中,并将其样式更改为 Black Opaque 时,取消按钮会保持不合适的蓝色/灰色并且不会变成黑色。

How can I make the cancel button black?如何使取消按钮变黑?

EDIT: It does work like this:编辑:它确实像这样工作:

// Assume a UISearchBar searchBar.
NSArray *subviews = [searchBar subviews];

// The index depends on how you configure the searchBar.
UIButton *cancelButton = [subviews objectAtIndex:3];

// Set the style to "normal" style.
[cancelButton setStyle:0];

But the setStyle: method is from a private framework, so this might be an issue when submitting the app to Apple.但是setStyle:方法来自一个私有框架,所以在向 Apple 提交应用程序时这可能是一个问题。

I used some thing like this and worked with me:我使用了这样的东西并与我一起工作:

[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor blackColor]];

it changed the cancel button color to black.它将取消按钮颜色更改为黑色。

Update for iOS 9.0, the method appearanceWhenContainedIn is deprecated, use appearanceWhenContainedInInstancesOfClasses instead:更新的iOS 9.0,该方法appearanceWhenContainedIn已过时,使用appearanceWhenContainedInInstancesOfClasses代替:

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor blackColor]];

And in Swift 3:在 Swift 3 中:

UIBarButtonItem.appearance(whenContainedInInstancesOf:[UISearchBar.self]).tintColor = UIColor.black

The problem with your solution is that the code is assuming that the objectAtIndex:3 is the cancel button.您的解决方案的问题在于代码假设 objectAtIndex:3 是取消按钮。 Not only does this generate a compiler warning, but also if you are displaying the Cancel button programmatically (for example using [searchBar setShowsCancelButton:YES] , you risk crashing the application.这不仅会生成编译器警告,而且如果您以编程方式显示“取消”按钮(例如使用[searchBar setShowsCancelButton:YES] ,则可能会导致应用程序崩溃。

A simpler solution is to set the style of the whole search bar in ViewDidLoad(), using:一个更简单的解决方案是在 ViewDidLoad() 中设置整个搜索栏的样式,使用:

searchBar.tintColor = [UIColor colorWithWhite:0.3 alpha:1.0];

this overrides the style set in the Interface Builder BUT also changes the colour of the Cancel button to be same colour as the whole bar (although it doesn't let you set the style of Cancel button independently, unfortunately.这会覆盖在界面生成器中设置的样式,但也会将取消按钮的颜色更改为与整个栏的颜色相同(尽管不幸的是,它不允许您独立设置取消按钮的样式。

Try this and see: (I tested below code with Swift 4.1 - Xcode 9.3-beta4 )试试这个看看:(我用 Swift 4.1 - Xcode 9.3-beta4测试了下面的代码)

@IBOutlet weak var sbSearchBar: UISearchBar!

sbSearchBar.showsCancelButton = true
sbSearchBar.barTintColor = UIColor.blue
sbSearchBar.tintColor = UIColor.red

if let buttonItem = sbSearchBar.subviews.first?.subviews.last as? UIButton {
    buttonItem.setTitleColor(UIColor.yellow, for: .normal)
}

在此处输入图片说明

In Swift 4.2在 Swift 4.2 中

let appearance = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
appearance.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor(named: "goldColor")!], for: .normal)

This works for me.这对我有用。 Thanks @Tim Semple谢谢@Tim Semple

for iOS 10:对于 iOS 10:

UISearchBar.appearance().tintColor = UIColor.red //cancel button color
UISearchBar.appearance().barTintColor = UIColor.blue //background button color

This is an updated version of Hossam Ghareeb's answer above for Swift 3:这是上面针对 Swift 3 的Hossam Ghareeb 回答的更新版本:

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

But this will not override appearance if it has already been set elsewhere for UIBarButtonItem.但是,如果已经在别处为 UIBarButtonItem 设置了外观,则这不会覆盖外观。

For example, in my navbar controller I had to change this:例如,在我的导航栏控制器中,我不得不改变这一点:

UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.white], for: UIControlState.normal)

To this for the solution above to work:为此,上述解决方案可以正常工作:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self] ).setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.white], for: UIControlState.normal)

I have taken Benjamin's answer and combined it with safe Array lookup to produce a short, but safe functional version:我已经采用了Benjamin 的答案,并将其与安全Array查找相结合,以生成一个简短但安全的功能版本:

searchController.searchBar.tintColor = UIColor.whiteColor()
(searchController.searchBar.subviews[safe: 0]?.subviews as? [UIView])?
    .filter({$0.isKindOfClass(UITextField)})
    .map({$0.tintColor = .lightGrayColor()})

This results in coloring the Cancel button white and the cursor when typing gray.这会导致在键入灰色时将取消按钮和光标着色为白色。 Otherwise it would be white and thus not seen.否则它会是白色的,因此看不到。 The searchController is an object of type UISearchController . searchControllerUISearchController类型的对象。 If anybody wants to use it inside the results controller, replace it with self .如果有人想在结果控制器中使用它,请将其替换为self

The implementation of the safe: subscript is nkukushkin's answer: safe:下标的实现是nkukushkin 的回答:

extension Array {
    subscript(safe index: Int) -> T? {
        return indices(self) ~= index ? self[index] : nil
    }
}

Click on Search bar and set the tint color under view from Interface Builder.单击搜索栏并在界面生成器的视图下设置色调颜色。

在此处输入图片说明

let view: UIView = self.searchBar.subviews[0] as UIView
let subViewsArray = view.subviews

for subView: UIView in subViewsArray {
    if let cancelButt = subView as? UIButton{
        cancelButt.setTitleColor(UIColor.white, for: .normal)         
    }
}

This worked for me这对我有用

Came up with a following solution and it is working on iOS 13.0 and iOS 12.4 as well, must be working on prior versions to till iOS 9.0.提出了以下解决方案,它也适用于 iOS 13.0 和 iOS 12.4,必须适用于之前的版本,直到 iOS 9.0。 The following solution is for:以下解决方案适用于:

  1. Cancel Button Color (Normal State).取消按钮颜色(正常状态)。
  2. Cancel Button Color (Disabled State).取消按钮颜色(禁用状态)。
  3. Search Bar Text Field Background Color (Normal State).搜索栏文本字段背景颜色(正常状态)。

For Objective C:对于目标 C:

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor whiteColor]]; 

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateDisabled];

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:@{NSBackgroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];

The above code also fixed my UI issues for iOS 13 and iPhone X. I included this code in my AppDelegate.m class in didFinishLaunchingWithOptions function, so that the changes could be done in the whole app.上面的代码还修复了 iOS 13 和 iPhone X 的 UI 问题。我将此代码包含在我的AppDelegate.m类中的didFinishLaunchingWithOptions函数中,以便可以在整个应用程序中完成更改。

For those looking to reproduce the same behavior in Swift :对于那些希望在 Swift 中重现相同行为的人:

override func viewWillAppear(animated: Bool) {
    self.searchBar.tintColor = UIColor.whiteColor()

    let view: UIView = self.searchBar.subviews[0] as! UIView
    let subViewsArray = view.subviews

    for (subView: UIView) in subViewsArray as! [UIView] {
        println(subView)
        if subView.isKindOfClass(UITextField){
            subView.tintColor = UIColor.blueColor()
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM