2013-10-29 42 views
1

我想要将Textfield的色调更改为蓝色并取消UISearchBar的按钮色调颜色为白色。如何更改iOS7中UISearchBar的取消按钮色调颜色

我正在使用下面的代码。

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]]; 
     } 
    } 
} 

但这同时更改文本字段和取消按钮的色调颜色为白色。任何人都可以提出另一种方法吗?

这里就是我得到...

enter image description here

文本字段的色调颜色也白.....

回答

13

尝试一些事情是这样的:

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],UITextAttributeTextShadowOffset,nil] forState:UIControlStateNormal]; 
+0

@iBhavesh你必须在-didFinishLaunchingWithOptions补充一点: – OMK

+0

这是为什么不工作iOS9? – skyline75489

0

请在您的方法中包含以下行 - didFinishLanchingWithOptions:

[[UIBarButtonItem appearance] setBackgroundImage:[[UIImage imageNamed:@"Your image.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; 
3
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]; 
+0

在iOS 7中不起作用 –

0
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];