将UISearchBar背景更改为自定义图像的正确方法是什么



我知道以前有人问过这个问题,我找到了这个解决方案:

for (UIView *subview in searchBar.subviews) {
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
        bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top-bar.png"]];
        [searchBar insertSubview:bg aboveSubview:subview];
        [subview removeFromSuperview];
        break;
    }
}

我只是不确定它是否包含一些苹果会拒绝的私有 API,或者有更好的方法来做到这一点?

  • 我仍然支持 ios 4,所以没有只有 ios 5 的 API

Apple 不鼓励使用私有未记录 API 的人。

但是,您仍然可以在应用程序中使用子类化功能。我使用过这种子类化UISearchBarBackground。苹果接受了我的申请,这是实时的。因此,在您的应用程序中使用上述代码没有问题。

相关内容

最新更新