我知道如何删除/更改UISearchBar搜索字段周围的背景颜色:
[[self.searchBar.subviews objectAtIndex:0] removeFromSuperview];
self.searchBar.backgroundColor = [UIColor grayColor];
Run Code Online (Sandbox Code Playgroud)

但是不知道如何在里面这样做:

这需要与iOS 4.3+兼容.
我现在已经挣扎了一段时间.已经到处搜索,但提供的解决方案仅适用于objective-c.其中包括
UITextField *txt = [_searchBar valueForKey:@"_searchField"];
虽然有些人可能会说这是受保护的API,Apple可能会拒绝使用此类代码的应用程序.
所以,现在我已经尝试了很多方法来解决这个问题,但它根本不起作用.我的代码是这样的:
searchBar = UISearchBar(frame: CGRectMake(0, 0, 320.0, 30.0))
searchBar.autoresizingMask = UIViewAutoresizing.FlexibleWidth
searchBar.searchBarStyle = UISearchBarStyle.Minimal
searchBar.layer.cornerRadius = 15
searchBar.delegate = self
searchBar.backgroundColor = UIColor.whiteColor()
if floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1{
// Load resources for iOS 6.1 or earlier
searchBar.placeholder = "Search";
} else {
// The following "hack" (if we can call that) breaks the UI for different size devices.
// Load resources for iOS 7 or later
searchBar.placeholder = "Search ";
}
Run Code Online (Sandbox Code Playgroud)
这给了我奇怪的结果:

虽然我想要的只是UISearchBar里面的文本字段有白色背景,而SearchBar本身的cornerRadius比如15.
我怎样才能做到这一点? …