2013-10-29 158 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]];