UINavigationItem 中的搜索栏

发布于 2024-11-28 08:07:58 字数 443 浏览 4 评论 0原文

我使用以下代码在 UINavigationItem 中添加了一个搜索栏:

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[searchBar sizeToFit];
searchBar.delegate = self;
self.navigationItem.titleView = searchBar;
self.navigationItem.title = self.category.title;
[searchBar release];

但结果 UI 如下所示:

在此处输入图像描述

如何我可以更改搜索栏的颜色并使其与导航栏的背景相同吗?

谢谢。

I added a search bar in the UINavigationItem using the following code:

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[searchBar sizeToFit];
searchBar.delegate = self;
self.navigationItem.titleView = searchBar;
self.navigationItem.title = self.category.title;
[searchBar release];

but the result UI is like this:

enter image description here

How can I change the color of the search bar and make it the same with the background of the navigation bar?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事与诗 2024-12-05 08:07:58

对于 UINavigationBar,有多种方法可以操纵它的背景颜色:

  1. 将tintColor 属性设置为您需要的颜色。
  2. 按照建议的修改方式应用自定义渐变或其他图案图像 -  http://leonov.co/2011/04/uinavigationbar-and-uitoolbar-customization-ultimate-solution/

对于搜索栏tintColor 是也可用。但在你的情况下,我建议继承 UISearchBar 并提供如下所示的实现,以完全摆脱后台绘制例程。在这种情况下,您将在透明工具栏上获得搜索栏,并且您将只需要管理 UINavigationBar 颜色。

//TransparentSearchBar.m

@interface TransparentSearchBar()
- (void)removeBackgroundView;
@end

@implementation TransparentSearchBar

- (id)init
{
    self = [super init];

    if(self) {
        [self removeBackgroundView];
    }

    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder 
{
    self = [super initWithCoder:aDecoder];

    if(self) {
        [self removeBackgroundView];
    }

    return self;
}

- (id)initWithFrame:(CGRect)frame 
{
    self = [super initWithFrame:frame];

    if(self) {
        [self removeBackgroundView];
    }

    return self;
}

- (void)removeBackgroundView 
{
    for (UIView *view in self.subviews)
    {
        if ([view isKindOfClass:NSClassFromString
             (@"UISearchBarBackground")])
        {
            [view removeFromSuperview];
            break;
        }
    }
}

@end

For a UINavigationBar there is multiple ways to manipulate background color of it:

  1. Set a tintColor property to a color that you need.
  2. Follow proposed way of modification to apply custom gradient or other pattern image — http://leonov.co/2011/04/uinavigationbar-and-uitoolbar-customization-ultimate-solution/

For a search bar tintColor is also available. But in your case I suggest to inherit form UISearchBar and provide implementation as shown below to get rid from background drawing routine totally. In this case you will get search bar on an transparent toolbar and will and you will need to manage only UINavigationBar color.

//TransparentSearchBar.m

@interface TransparentSearchBar()
- (void)removeBackgroundView;
@end

@implementation TransparentSearchBar

- (id)init
{
    self = [super init];

    if(self) {
        [self removeBackgroundView];
    }

    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder 
{
    self = [super initWithCoder:aDecoder];

    if(self) {
        [self removeBackgroundView];
    }

    return self;
}

- (id)initWithFrame:(CGRect)frame 
{
    self = [super initWithFrame:frame];

    if(self) {
        [self removeBackgroundView];
    }

    return self;
}

- (void)removeBackgroundView 
{
    for (UIView *view in self.subviews)
    {
        if ([view isKindOfClass:NSClassFromString
             (@"UISearchBarBackground")])
        {
            [view removeFromSuperview];
            break;
        }
    }
}

@end
后知后觉 2024-12-05 08:07:58

如果您查看 UISearchBar 上的文档,它具有属性“tintColor”。
将其设置为导航栏的颜色。

如果您不知道导航栏的颜色,请获取导航栏的tintColor。

您可能还想使用搜索栏的“半透明”属性。可能会工作得更好。

If you see the documentation on UISearchBar it has the property "tintColor".
Set that to color of the navigation bar.

If you do not know the color of navigationbar, get the tintColor of navigationBar.

You might also want to lay with the "translucent" property of the searchbar. Might work better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文