【问题标题】:How to change cancel button tint color of UISearchBar in iOS7如何在 iOS7 中更改 UISearchBar 的取消按钮颜色
【发布时间】:2013-10-29 06:39:43
【问题描述】:

我想将 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的tint color也是White.....

【问题讨论】:

    标签: ios iphone objective-c uisearchbar


    【解决方案1】:

    试试这样的:

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

    【讨论】:

    • @iBhavesh 你必须在 -didFinishLaunchingWithOptions 添加这个:
    【解决方案2】:
    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];
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:
        [[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-10
          • 2011-02-16
          • 2013-10-18
          • 1970-01-01
          • 2013-10-12
          相关资源
          最近更新 更多