2012-06-18 44 views
1

我知道人们面前问这个,我发现这个解决方案:将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
+0

可能重复的[如何自定义UISearchBar](http://stackoverflow.com/questions/6201406/how-to-custom-uisearchbar) –

回答

1

苹果并不鼓励谁在使​​用私人无证的API人。

但是你仍然可以在你的应用程序中使用子类化功能。我已经使用了这种UISearchBarBackground子类。苹果公司接受了我的现场应用程序。所以在你的应用程序中没有使用上述代码的问题。

+0

你知道更好的方法吗? – Eyal

+0

我猜想IOS 5中只有更好的方法可用。我现在不记得了。 – Splendid

相关问题