ios UISearchBar 取消按钮颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2787481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UISearchBar cancel button color?
提问by cactus
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 时这可能是一个问题。
回答by Hossam Ghareeb
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.
它将取消按钮颜色更改为黑色。
Updatefor 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
回答by Alun
The problem with your solution is that the code is assuming that the objectAtIndex:3 is the cancel button. 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.
您的解决方案的问题在于代码假设 objectAtIndex:3 是取消按钮。这不仅会生成编译器警告,而且如果您以编程方式显示“取消”按钮(例如使用[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.
这会覆盖在界面生成器中设置的样式,但也会将取消按钮的颜色更改为与整个栏的颜色相同(尽管不幸的是,它不允许您独立设置取消按钮的样式。
回答by Krunal
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.tintColor = UIColor.red
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)
}
回答by black_pearl
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
回答by Tim Semple
This is an updated version of Hossam Ghareeb's answer abovefor 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)
回答by MobileMon
for iOS 10:
对于 iOS 10:
UISearchBar.appearance().tintColor = UIColor.red //cancel button color
UISearchBar.appearance().barTintColor = UIColor.blue //background button color
回答by Michal
I have taken Benjamin'sanswer and combined it with safe Array
lookup to produce a short, but safe functionalversion:
我已经采用了Benjamin 的答案,并将其与安全Array
查找相结合,以生成一个简短但安全的功能版本:
searchController.searchBar.tintColor = UIColor.whiteColor()
(searchController.searchBar.subviews[safe: 0]?.subviews as? [UIView])?
.filter({extension Array {
subscript(safe index: Int) -> T? {
return indices(self) ~= index ? self[index] : nil
}
}
.isKindOfClass(UITextField)})
.map({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)
}
}
.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
. If anybody wants to use it inside the results controller, replace it with self
.
这会导致在键入灰色时将取消按钮和光标着色为白色。否则它会是白色的,因此看不见。的searchController
是类型的对象UISearchController
。如果有人想在结果控制器中使用它,请将其替换为self
.
The implementation of the safe:
subscript is nkukushkin'sanswer:
safe:
下标的实现是nkukushkin的回答:
[[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];
回答by Rishab
回答by Ghadeer
This worked for me
这对我有用
回答by Syed Faizan Ahmed
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. The following solution is for:
提出了以下解决方案,它也适用于 iOS 13.0 和 iOS 12.4,必须适用于之前的版本,直到 iOS 9.0。以下解决方案适用于:
- Cancel Button Color (Normal State).
- Cancel Button Color (Disabled State).
- Search Bar Text Field Background Color (Normal State).
- 取消按钮颜色(正常状态)。
- 取消按钮颜色(禁用状态)。
- 搜索栏文本字段背景颜色(正常状态)。
For Objective C:
对于目标 C:
##代码##The above code also fixed my UI issues for iOS 13 and iPhone X. I included this code in my AppDelegate.mclass in didFinishLaunchingWithOptionsfunction, so that the changes could be done in the whole app.
上面的代码还修复了 iOS 13 和 iPhone X 的 UI 问题。我将此代码包含在我的AppDelegate.m类中的didFinishLaunchingWithOptions函数中,以便可以在整个应用程序中完成更改。