如何更改iOS7中UISearchBar的取消button色调颜色
我想将Textfield的色彩颜色更改为蓝色,并将UISearchBar的button色调颜色取消为白色。
我正在使用下面的代码。
for (UIView *subView in searchBar.subviews) { for (UIView *ndLeveSubView in subView.subviews) { if([ndLeveSubView isKindOfClass:[UITextField class]]) { [(UITextField *)subView setTintColor:[UIColor blueColor]]; } else if ([ndLeveSubView isKindOfClass:[UIButton class]]) { [(UIButton *)subView setTintColor:[UIColor whiteColor]]; } } }
但是这改变了Textfield和取消button的色调为白色。 任何人都可以提出另一种方法吗?
这是我得到的…
TextField的色调也是白色…..
尝试一下这样的事情:
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],UITextAttributeTextShadowOffset,nil] forState:UIControlStateNormal];
UITextAttributeTextColor
已经在iOS 7 SDK中被弃用了。 改用NSForegroundColorAttributeName
。 像这样:
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor],NSForegroundColorAttributeName, [NSValue valueWithUIOffset:UIOffsetMake(0, 1)],NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
如果你的UISearchBar在UINavigationBar中 – 你可以这样做:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whitecolor] ,NSForegroundColorAttributeName, [NSValue valueWithUIOffset:UIOffsetMake(0, 1)],NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
请在您的方法didFinishLanchingWithOptions:
包含以下行didFinishLanchingWithOptions:
[[UIBarButtonItem appearance] setBackgroundImage:[[UIImage imageNamed:@"Your image.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];