我想从UISearchBar中删除clear按钮(灰色x)。我试着按照this answer中描述的那样做,但它不起作用。
我将以下答案和注释中的Objective-C代码翻译为以下Swift代码:
for subview in searchBar.subviews {
configureSearchBarView(subview as UIView)
}
func configureSearchBarView(view: UIView) {
for subview in view.subviews {
self.configureSearchBarView(subview as UIView)
}
if subview.conformsToProtocol(UITextInputTraits) {
var clearView = view as UITextField
clearView.clearButtonMode = UITextFieldViewMode.Never
}
}
不幸的是,这并没有移除clear按钮。
Another answer建议使用appearanceWhenContainedIn
,但这种方法似乎没有在Swift中实现。
有什么想法吗?
发布于 2014-08-21 14:47:08
我找到了一个解决方案。可以将clear按钮与自定义图像进行交换:
UISearchBar.appearance().setImage(UIImage(named: "emptyImg"), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)
UISearchBar.appearance().setImage(UIImage(named: "emptyImg"), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Highlighted)
emptyImg
是一个包含一个白色像素的png。在this answer中找到
https://stackoverflow.com/questions/25407594
复制相似问题