Iphone UISearchBar中范围按钮文本的颜色
我有一个UISearchBar,它必须是灰色的 我现在遇到的问题是,一旦我设置了任何着色颜色,范围按钮就会得到相同的字体颜色,如果没有选择按钮,就会产生非常差的对比度 如果未设置色调,则颜色会根据所选状态而变化。有没有办法做到这一点,使用色调 默认值 使用色调 更新 使用Iphone UISearchBar中范围按钮文本的颜色,iphone,ios,objective-c,xcode,uisearchbar,Iphone,Ios,Objective C,Xcode,Uisearchbar,我有一个UISearchBar,它必须是灰色的 我现在遇到的问题是,一旦我设置了任何着色颜色,范围按钮就会得到相同的字体颜色,如果没有选择按钮,就会产生非常差的对比度 如果未设置色调,则颜色会根据所选状态而变化。有没有办法做到这一点,使用色调 默认值 使用色调 更新 使用po[mySearchBar recursiveDescription]我发现UISearchbar具有以下视图层次结构: <UISegmentedControl> <_UISegmentedCon
po[mySearchBar recursiveDescription]
我发现UISearchbar
具有以下视图层次结构:
<UISegmentedControl>
<_UISegmentedControlBackgroundView>
<UIImageView>
<UISegment>
<UISegmentLabel>
<UIImageView>
<UISegment>
<UISegmentLabel>
<UIImageView>
<UISegment>
<UISegmentLabel>
<UIImageView>
<UISearchBarBackground>
我似乎记得在使用色调时遇到过类似的问题。一个不幸的副作用似乎是它默认了它所影响的UIKit元素的颜色相关属性 我不知道在使用色调时是否有办法防止这种缺陷(我认为没有),但以下解决方法可能对您有用: iOS 6及以下版本 iOS 7+ (来源:@willyang)
使用
UISearchBar
的SetScopeBarButtontTitletExtAttributes:forState:
方法,您可以配置范围栏按钮标题的属性,包括文本颜色。如果我正确回答了您的问题,那么这就是您要做的
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];
}
}
由于IOS 7中不推荐使用UITextAttributeTextColor,因此应改用NSForegroundColorAttributeName:
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
参考:Swift 3:
如果以编程方式构建搜索栏,则可以使用以下设置范围栏的TextAttributes:
let searchController = UISearchController(searchResultsController: nil) // inside the class
您可以查看UISearchBar方法(向下滚动一半以查找ScopeBar方法)以进行自定义:
你是说取消按钮的颜色吗?@AppleDelegate取消按钮在我的情况下不是问题,因为那里的对比度很好。我想改变的范围按钮(动物,植物,其他)非常优雅的解决方案的颜色。谢谢这和这个问题的答案是一样的question@Besi和哪个答案一样?这个答案是一个更新,而不仅仅是一个副本。@Besi是的,我知道。但情况不同。这个答案将适用于iOS 7,因为iOS7中不推荐使用UITextAttributeTextColor。好的,我错过了那个小细节。你能编辑你的答案吗?否则我不能删除我的反对票。
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
let searchController = UISearchController(searchResultsController: nil) // inside the class
// Background color
searchController.searchBar.setScopeBarButtonTitleTextAttributes([NSBackgroundColorAttributeName: UIColor.yellow ], for: UIControlState.normal)