我已经为此苦苦挣扎了一段时间。到处搜索,但提供的解决方案只适用于objective-c。
UITextField *txt = [_searchBar valueForKey:@"_searchField"];
有人可能会说这是受保护的API,苹果可能会拒绝带有此代码的应用程序。
因此,我已经尝试了很多方法来解决这个问题,但都没有成功。我的代码如下:
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 ";
}
这给我带来了奇怪的结果:![enter image description here](https://istack.dev59.com/yNyoa.webp)