简体   繁体   English

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

[英]What is the proper way to change UISearchBar background to custom image?

I know people asked this before and I found this solution: 我知道人们以前曾问过这个问题,后来我找到了解决方案:

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;
    }
}

I'm just not sure if it include some private API that apple will reject, or is there a better way to do this? 我只是不确定它是否包含苹果会拒绝的某些私有API,还是有更好的方法来做到这一点?

  • I'm still supporting ios 4, so no ios 5 only APIs 我仍然支持ios 4,因此没有仅ios 5的API

Apple does not encourage people who are using private undocumented apis. 苹果公司不鼓励使用私人无证API的人。

But still you can use subclassing feature in your application. 但是仍然可以在应用程序中使用子类化功能。 I have used this kind of subclassing UISearchBarBackground. 我使用了这种子类化UISearchBarBackground。 Apple was accepted my application which was in live. Apple接受了我的实时申请。 So no issues of using above code in your application. 因此,在您的应用程序中使用上述代码没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM