Bes*_*esi 3 iphone xcode objective-c uisearchbar ios
我有一个UISearchBar,它必须有灰色调.
我现在遇到的问题是,一旦我设置了任何色调颜色,范围按钮就会得到相同的字体颜色,如果没有选择按钮,会导致对比度非常差.
如果未设置色调,则颜色会根据所选状态而有所不同.有没有办法实现这一点,使用淡色?


使用po [mySearchBar recursiveDescription]我发现它UISearchbar具有以下视图层次结构:
<UISegmentedControl>
<_UISegmentedControlBackgroundView>
<UIImageView>
<UISegment>
<UISegmentLabel>
<UIImageView>
<UISegment>
<UISegmentLabel>
<UIImageView>
<UISegment>
<UISegmentLabel>
<UIImageView>
<UISearchBarBackground>
Run Code Online (Sandbox Code Playgroud)
Ell*_*rry 10
我似乎记得在使用色调时会遇到类似的问题.看起来不幸的副作用是它默认与其影响的UIKit元素的颜色相关属性.
我不知道在使用色调时是否有办法防止此缺陷(我认为没有),但以下解决方法可能对您有用:
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], UITextAttributeTextColor, nil] forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)
(信用:@willyang)
使用UISearchBar的setScopeBarButtonTitleTextAttributes:forState:方法,你可以配置范围栏按钮标题的属性,包括文本颜色.
由于UITextAttributeTextColor在IOS 7过时,应该用NSForegroundColorAttributeName改为:
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)
参考:http://www.downstairz.net/2013/11/24/uitextattributetextcolor-is-deprecated-in-ios-7/