如何在iOS7中更改UISearchBar的取消按钮色调颜色

时间:2013-10-29 06:39:44

标签: ios iphone objective-c uisearchbar

我想将Textfield的色调颜色更改为蓝色,并取消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]];
        }
    }
}

但是这会将Textfield和取消按钮的色调颜色更改为白色。任何人都可以提出另一种方法吗?

这就是我得到的......

enter image description here

TextField的色调也是白色......

3 个答案:

答案 0 :(得分:13)

尝试这样的事情:

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

答案 1 :(得分:4)

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

答案 2 :(得分:0)

请在方法中添加以下一行 - didFinishLanchingWithOptions:

[[UIBarButtonItem appearance] setBackgroundImage:[[UIImage imageNamed:@"Your image.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];