先看效果
ios13之后多了个新的属性:searchTextField
所以iOS13之后的可以直接拿
代码如下
lazy var searchToolBar : UISearchBar = {
letsearchBar =UISearchBar.init(frame:CGRect(x:15, y:kStatusBarHeight, width:kScreenWidth-75, height:32))
searchBar.isTranslucent=true
searchBar.placeholder="输入游戏名"
searchBar.autocorrectionType= .no
searchBar.autocapitalizationType = .none
searchBar.searchBarStyle= .default
searchBar.layer.cornerRadius=16
searchBar.layer.masksToBounds=true
searchBar.barTintColor=UIColor.color(hex:"#f0f0f0")
if#available(iOS13.0, *) {
searchBar.searchTextField.backgroundColor=UIColor.color(hex:"#f0f0f0")
}else{
forsubView :UIViewinsearchBar.subviews.last!.subviews{
ifsubView.isKind(of:NSClassFromString("UISearchBarTextField")!) {
lettextField :UITextField= subViewas!UITextField
textField.backgroundColor=UIColor.color(hex:"#f0f0f0")
textField.textColor=DarkColor()
textField.font=UIFont.systemFont(ofSize:13)
}
}
}
returnsearchBar
}()
网友评论