隐藏UISearchBar明文按钮
我想知道如何隐藏或不显示出现在UISearchBar
的textField
的UISearchBar
交叉
我试过用这个
filterSearchBar.showsCancelButton = NO;
然而,这是一个实际的取消按钮,而不是小的灰色十字架,所以我想知道UISearchBar
中显示的小灰色按钮是否等效。
任何帮助,将不胜感激。
您需要获取搜索栏的textField:
UITextField *textField = [searchBar valueForKey:@"_searchField"]; textField.clearButtonMode = UITextFieldViewModeNever;
希望这个帮助! =)
有一种更好的方法,您不必使用私有API或遍历子视图来执行此操作,因此此解决方案是App Store安全的。
UISearchBar有一个内置的API来执行此操作:
[UISearchBar setImage:forSearchBarIcon:state]
您想要的SearchBar图标键是UISearchBarIconClear,您需要UIControlStateNormal状态。 然后给它一个清晰的图像图像,你就完成了。
所以,它应该是这样的:
[searchBar setImage:clearImage forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
根据@Gines的回答,这是Swift版本:
func searchBarTextDidBeginEditing(searchBar: UISearchBar) { guard let firstSubview = searchBar.subviews.first else { return } firstSubview.subviews.forEach { ($0 as? UITextField)?.clearButtonMode = .never } }
您可以删除所有UISearchBar实例的明文按钮:
[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].clearButtonMode = UITextFieldViewModeNever;
Swift 3,基于@Alexsander回答:
searchBar.setImage(UIImage(), for: .clear, state: .normal)
斯威夫特4
在清除按钮上添加Alexander的答案并阻止用户交互:
要隐藏按钮:
searchBar.setImage(UIImage(), for: .clear, state: .normal)
要在清除按钮上禁用用户交互,只需子类化UISearchBar
class CustomSearchBar: UISearchBar { override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { let view = super.hitTest(point, with: event) if view is UIButton { return view?.superview // this will pass-through all touches that would've been sent to the button } return view } }
我尝试了关于这个问题的不同解决方案,即使是在这篇文章中选择的那个,但是它们没有用。
这是我找到解决此问题的方法:
UIView *subview = [[searchBar subviews] firstObject]; //SearchBar only have one subview (UIView) //There are three sub subviews (UISearchBarBackground, UINavigationButton, UISearchBarTextField) for (UIView *subsubview in subview.subviews) { //The UISearchBarTextField class is a UITextField. We can't use UISearchBarTextField directly here. if ([subsubview isKindOfClass: [UITextField class]]) { [(UITextField *)subsubview setClearButtonMode:UITextFieldViewModeNever]; } }
尝试这个:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { UITextField *textField = [searchBar valueForKey:@"_searchField"]; textField.clearButtonMode = UITextFieldViewModeNever; }
Swift 3解决方案:
extension UISearchBar{ var textField : UITextField{ return self.value(forKey: "_searchField") as! UITextField } }
用法:
searchBar.textField.clearButtonMode = .never
在Swift 2.3的情况下,只需使用:
var searchBar = UISearchBar(); searchBar.frame = CGRectMake(0, 0, 00, 20)) for subview: UIView in (searchBar.subviews.first?.subviews)! { if (subview.isKindOfClass(UITextField) ) { let textFieldObject = (subview as! UITextField) textFieldObject.clearButtonMode = .Never; } }
Swift 2.3,基于@Alexsander回答:
searchBar.setImage(UIImage(named: "SearchClearIcon"), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Highlighted) searchBar.setImage(UIImage(named: "SearchClearIcon"), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)
将Soto_iGhost的答案转换为Swift 4:
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { let textField: UITextField = searchBar.value(forKey: "_searchField") as! UITextField textField.clearButtonMode = .never }
如果你有一个UISearchBar
的出口,你可以在你class上的任何地方写上面的代码。
- 如何构建React Native应用
- ipa app无法添加到我们的itunes库中,它不是一个有效的应用程序
- iOS的Model View Controller dengan contoh
- [memo] [XIB]用于可重用组件的Xcode 5中的Xib文件中的自定义UIView-第1部分
- Obscure Xcode build error:“文件”myapp-SBPartialInfo.plist“无法打开,因为没有这样的文件。
- iOS:无法使BluetoothManager正常工作
- parsing,如何计数,将数据放入数组中,然后显示数组的内容
- WaitUntilDOne在performSelectorOnMainThread中有什么意义?
- HealthKit(iOS)不会在后台传递数据(objC)