对于您的第一个问题,您可以访问UITextField
您的UISearchbar
并编辑占位符颜色:
var textfield:UITextField = searchBar.valueForKey("searchField") as UITextField
//Set the foregroundcolor of the placeholder
var attributedString = NSAttributedString(string: "your placeholdertext", attributes: [NSForegroundColorAttributeName : UIColor.whiteColor()])
textfield.attributedPlaceholder = attributedString
然后还要设置您必须访问的玻璃leftView
颜色UITextField
:
//Get the glass icon
var iconView:UIImageView = textfield.leftView as UIImageView
//Make the icon to a template which you can edit
iconView.image = iconView.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
//Set the color of the icon
iconView.tintColor = UIColor.whiteColor()
要隐藏您的取消按钮,您UISearchbar
可以使用searchBarShouldBeginEditing
委托方法并将其隐藏。只需确保添加第UISearchBarDelegate
一个并将委托设置为您的搜索栏:
class YourviewController:UIViewController, UISearchBarDelegate{
override func viewDidLoad() {
searchbar.delegate = self
}
func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
searchBar.setShowsCancelButton(false, animated: true)
return true
}
}