xcode UISearchBar 中范围按钮文本的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15385050/
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
Color of scope button text in UISearchBar
提问by Besi
I have a UISearchBar which has to have a gray tint.
我有一个 UISearchBar,它必须是灰色的。
The problem I am having now is, that as soon as I set anytint color, the scope buttons get the same font color which results in a very poor contrast, if the buttons aren't selected.
我现在遇到的问题是,一旦我设置了任何色调颜色,示波器按钮就会获得相同的字体颜色,如果未选择按钮,则会导致对比度非常差。
If no tint is set, then the color vary depending on the selected state. Is there a way to achieve this, using a tint color?
如果未设置色调,则颜色会因所选状态而异。有没有办法使用淡色来实现这一点?
The default
默认的
Using a tint
使用色调
Update
更新
Using po [mySearchBar recursiveDescription]
I figured out that the UISearchbar
has the following view hierarchy:
使用po [mySearchBar recursiveDescription]
我发现UISearchbar
具有以下视图层次结构:
<UISegmentedControl>
<_UISegmentedControlBackgroundView>
<UIImageView>
<UISegment>
<UISegmentLabel>
<UIImageView>
<UISegment>
<UISegmentLabel>
<UIImageView>
<UISegment>
<UISegmentLabel>
<UIImageView>
<UISearchBarBackground>
回答by Elliott James Perry
I seem to recall running into similar issues when using tint colours. It appears that an unfortunate side effect is that it defaults colour related properties of the UIKit elements it affects.
我似乎记得在使用色调颜色时遇到过类似的问题。似乎一个不幸的副作用是它默认了它影响的 UIKit 元素的颜色相关属性。
I don't know whether there is a way to prevent this defect when using tints (I don't think there is), but the following workaround may be useful to you:
我不知道在使用 tints 时是否有办法防止这种缺陷(我认为没有),但以下解决方法可能对您有用:
iOS 6 and below
iOS 6 及以下
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], UITextAttributeTextColor, nil] forState:UIControlStateSelected];
iOS 7+
iOS 7+
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
(Credit: @willyang)
(信用:@willyang)
Using the UISearchBar
's setScopeBarButtonTitleTextAttributes:forState:
method you can configure the attributes of the scope bar button titles, including the text colour.
使用UISearchBar
的setScopeBarButtonTitleTextAttributes:forState:
方法,您可以配置范围栏按钮标题的属性,包括文本颜色。
回答by Zhihao Yang
Because UITextAttributeTextColoris deprecated in IOS 7, should use NSForegroundColorAttributeNameinstead:
因为UITextAttributeTextColor在 IOS 7 中被弃用,应该使用NSForegroundColorAttributeName代替:
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
ref: http://www.downstairz.net/2013/11/24/uitextattributetextcolor-is-deprecated-in-ios-7/
参考:http: //www.downstairz.net/2013/11/24/uitextattributetextcolor-is-deprecated-in-ios-7/
回答by user3221892
Swift 3:
斯威夫特 3:
If you are building the SearchBar programmatically you can use the following to set the TextAttributes of the ScopeBar:
如果您以编程方式构建 SearchBar,您可以使用以下内容来设置 ScopeBar 的 TextAttributes:
let searchController = UISearchController(searchResultsController: nil) // inside the class
....
....
// Background color
searchController.searchBar.setScopeBarButtonTitleTextAttributes([NSBackgroundColorAttributeName: UIColor.yellow ], for: UIControlState.normal)
You can take a look at the UISearchBar methods (scroll halfway down to find the ScopeBar methods) to customize: https://developer.apple.com/reference/uikit/uisearchbar?language=objc
你可以看看 UISearchBar 方法(向下滚动到一半找到 ScopeBar 方法)来自定义:https: //developer.apple.com/reference/uikit/uisearchbar?language=objc
回答by Suhaiyl
If i got your question correctly then this is what you are trying to do
如果我正确回答了你的问题,那么这就是你想要做的
for (id subview in yourSearchDisplayController.searchBar.subviews )
{
if([subview isMemberOfClass:[UISegmentedControl class]])
{
UISegmentedControl *scopeBar=(UISegmentedControl *) subview;
[scopeBar setSegmentedControlStyle:UISegmentedControlStyleBar];
[scopeBar setTintColor: [UIColor redColor]];//you can also set RGB color here
//scopeBar.tintColor = [UIColor blackColor];
}
}