创建一个清晰的UISearchBar

时间:2014-04-13 10:53:17

标签: ios uisearchbar uitoolbar

我需要一个uisearchbar清楚

我已经尝试了所有建议的方法 - 将背景图像设置为清晰的等等。

但是 - UISearchbar似乎总是有一个框架 - 可以设置(使用图像方法)和带有色调的叠加(这是搜索栏中从搜索栏边界插入的部分)具有相同的高度和黑色 -

我无法使用任何搜索栏或工具栏方法摆脱这种色调 - 是否有一个黑客可以将其清除 - 或者将其拉伸到UISearchbar的所有角落???

这是我正在尝试的最后一个化身的片段 - 没有运气:(

self.searchBar = [[UISearchBar alloc] initWithFrame:searchBarBackground.bounds];
        self.searchBar.placeholder = NSLocalizedString(@"Search", nil);
        self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
        [searchBarBackground addSubview:self.searchBar];
        self.searchBar.translucent = YES;
        self.searchBar.backgroundImage = [UIImage new];
        self.searchBar.scopeBarBackgroundImage = [UIImage new];

添加搜索栏的层次结构转储

 <UISearchBar: 0x17f36340; frame = (0 0; 240 44); text = ''; opaque = NO; gestureRecognizers = <NSArray: 0x17f35e00>; layer = <CALayer: 0x17f36270>>
 | <UIView: 0x17f36010; frame = (0 0; 240 44); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x17f35fe0>>
 |    | <UISearchBarBackground: 0x17f35bc0; frame = (0 0; 240 44); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x17f35ac0>>
 |    |    | <_UIBackdropView: 0x17f317d0; frame = (0 0; 240 44); hidden = YES; opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <_UIBackdropViewLayer: 0x17f311e0>>
 |    | <UISearchBarTextField: 0x17f34e40; frame = (0 0; 0 0); text = ''; clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x17f34e10>>
 |    |    | <_UISearchBarSearchFieldBackgroundView: 0x17f337c0; frame = (0 0; 0 0); autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x17f33900>>
 |    |    |    | <_UISearchBarSearchFieldBackgroundView: 0x17f32540; frame = (0 0; 0 0); hidden = YES; opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x17f32480>>
 |    |    |    | <_UISearchBarSearchFieldBackgroundView: 0x17f31e00; frame = (0 0; 0 0); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x17f31ea0>>


 *

/

2 个答案:

答案 0 :(得分:2)

将我的UISearchbar的背景设置为“带alpha的白色”这样微不足道的事情令人头疼! - 这就是我最终能够创造出我想要的外观的方式。一个“清晰的”UISearchbar onwhitesh背景

self.searchBar = [[UISearchBar alloc] initWithFrame:SearchBarFrame];
        self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
        UIView *whiteView = [[UIView alloc] initWithFrame:self.searchBar.bounds];
        whiteView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5];
        whiteView.layer.cornerRadius = 5;

        [self.searchBar insertSubview:whiteView atIndex:0];

        self.searchBar.placeholder = NSLocalizedString(@"Search", nil);
        self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
        [self addSubview:self.searchBar];
        self.searchBar.translucent = YES;
        self.searchBar.backgroundImage = [UIImage new];
        self.searchBar.scopeBarBackgroundImage = [UIImage new];
        UIImage *clearImage = [UIImage imageWithColor:[UIColor clearColor] andSize:SearchBarFrame.size];
        [self.searchBar setSearchFieldBackgroundImage:clearImage forState:UIControlStateNormal];

辅助方法

+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    rect.size = size;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

答案 1 :(得分:0)

对于SWIFT:

  1. 创建清晰的PNG背景图片
  2. 将您的UISearchBar样式设置为默认值
  3. 在viewDidLoad中或您想要的任何位置添加此代码:

    yourSearchBar.setSearchFieldBackgroundImage(UIImage(named: "yourClearBackgroundImage"), forState: .Normal)
    yourSearchBar.backgroundImage = UIImage()