ios - 无法将 UISearchBar 的半透明属性设置为 NO

标签 ios objective-c ios7 uisearchbar bartintcolor

我正在尝试将 UISearchBarbarTintColor 设置为不透明。但是,设置 translucent 属性似乎没有任何作用。我在 bare-bones Xcode project here 中重现了这个问题.

self.searchDisplayController.searchBar.translucent = NO;
self.searchDisplayController.searchBar.barTintColor = [UIColor redColor];

上面的红色与 UIViews 中的 [UIColor redColor] 不一样,不是半透明的。我知道涉及在搜索栏上设置背景图片的解决方法,但上面的代码应该也能正常工作。

最佳答案

我下载了你的代码并找到了解决方案,添加一个方法名称是 removeUISearchBarBackgroundInViewHierarchy 并将 searchBar.backgroundColor 设置为 redColor。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.searchDisplayController.searchBar.translucent = NO;

    [self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
    self.searchDisplayController.searchBar.backgroundColor = [UIColor redColor];
}

- (void) removeUISearchBarBackgroundInViewHierarchy:(UIView *)view
{
    for (UIView *subview in [view subviews]) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [subview removeFromSuperview];
            break; //To avoid an extra loop as there is only one UISearchBarBackground
        } else {
            [self removeUISearchBarBackgroundInViewHierarchy:subview];
        }
    }
}

关于ios - 无法将 UISearchBar 的半透明属性设置为 NO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23556190/

相关文章:

ios - MKNetworkEngine 不触发它的方法

html - 停止在适用于 iOS 的 Phonegap 应用程序中自动检测电话号码(错误)

javascript - 双击在 ios 和 android (PhoneJS) 上不起作用

objective-c - C/Objective-C 中是否有任何 Lucene 包装器?

c++ - 寻找将 std::wstring 与 NSLog 一起使用的最便宜的方法

background - 如何检查是否在 iOS 上启用了 BackgroundFetching?

datepicker - NSInvalidArgumentException',原因: '-[UITableViewWrapperView indexPathForCell:]

ios - 具有 "More"按钮样式的 UIBarButtonItem

ios - CGAffineTransform 应用附加旋转

ios - writeToFile:atomically: atomically 是什么意思?