Iphone 点击取消按钮时,移除UI搜索栏右侧的清除按钮(灰色x)
好的,开始我的问题,下面是Spotify应用程序已经解决的问题的一些屏幕: Spotify的第1步:标准UI搜索栏未处于编辑模式 Spotify的第2步:UISearchBar现在处于编辑模式。输入搜索词。“取消”按钮从右侧滑入,然后出现“清除”按钮(灰色x) Spotify的第3步:按下取消按钮;键盘滑出,搜索栏不再处于编辑模式。搜索词保留,灰色x按钮现在隐藏 目前,按下“我的取消”按钮时,会触发以下代码:Iphone 点击取消按钮时,移除UI搜索栏右侧的清除按钮(灰色x),iphone,objective-c,ipad,interface-builder,Iphone,Objective C,Ipad,Interface Builder,好的,开始我的问题,下面是Spotify应用程序已经解决的问题的一些屏幕: Spotify的第1步:标准UI搜索栏未处于编辑模式 Spotify的第2步:UISearchBar现在处于编辑模式。输入搜索词。“取消”按钮从右侧滑入,然后出现“清除”按钮(灰色x) Spotify的第3步:按下取消按钮;键盘滑出,搜索栏不再处于编辑模式。搜索词保留,灰色x按钮现在隐藏 目前,按下“我的取消”按钮时,会触发以下代码: - (void)searchBarCancelButtonClicked:(UI
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO animated:YES];
}
其结果是:
我的第3步:搜索栏现在不处于编辑模式。取消按钮和键盘已滑出。搜索词仍然存在,但灰色x也存在
因此,我的问题是:如果-辞职第一响应者
(和-endEditing:
,仅供参考)在搜索栏中输入文本时不会隐藏灰色x按钮,如何隐藏它
再次感谢,朋友们。问题在于UISearchBar没有公开它的文本字段,而是管理文本字段本身的属性。有时,属性的值不是您想要的 例如,在我自己的应用程序中,我希望搜索栏的键盘样式使用透明警报样式 我的解决方案是遍历搜索栏的子视图,直到找到文本字段。然后,您应该能够在编辑时使用类似于
UITextFieldViewMode的内容作为参数来设置clearButtonMode
属性
这将使“清除”按钮仅在文本字段编辑时显示
您希望在viewdiload
或更早的时候执行此操作,因此在开始使用它之前(但在搜索栏初始化之后)会设置它
for (UIView *subview in searchBar.subviews)
{
if ([subview conformsToProtocol:@protocol(UITextInputTraits)])
{
[(UITextField *)subview setClearButtonMode:UITextFieldViewModeWhileEditing];
}
}
要扩展Jadariens的答案,如果您不希望灰色x出现,则需要使用以下选项
for (UIView *subview in searchBar.subviews)
{
if ([subview conformsToProtocol:@protocol(UITextInputTraits)])
{
[(UITextField *)subview setClearButtonMode:UITextFieldViewModeNever];
}
}
看起来iOS 7更改了UISearchBar的视图层次结构,并且文本框在视图中更深(上面的解决方案对我不起作用)。但是,修改上面的解决方案以遍历整个层次结构是可行的:
[self configureSearchBarView:[self searchBar]];
- (void)configureSearchBarView:(UIView*)view {
for (UIView *subview in [view subviews]){
[self configureSearchBarView:subview];
}
if ([view conformsToProtocol:@protocol(UITextInputTraits)]) {
[(UITextField *)view setClearButtonMode:UITextFieldViewModeWhileEditing];
}
}
我在前面的答案的基础上继续前进,因为我开始在iOS 7.1上看到崩溃,除非我做了以下更改。我为每个视图添加了一个额外的调用respondsToSelector
,以确保可以调用setClearButtonMode:
。我观察到UISearchBar
的一个实例被传入,这似乎是错误的符合UISearchBarTextField
协议,但没有setClearButtonMode:
选择器,因此发生了崩溃。还传入了UISearchBarTextField
的实例,它是要调用setClearButtonMode:
的实际对象
- (void)removeClearButtonFromView:(UIView *)view
{
if (!view)
{
return;
}
for (UIView *subview in view.subviews)
{
[self removeClearButtonFromView:subview];
}
if ([view conformsToProtocol:@protocol(UITextInputTraits)])
{
UITextField *textView = (UITextField *)view;
if ([textView respondsToSelector:@selector(setClearButtonMode:)])
{
[textView setClearButtonMode:UITextFieldViewModeNever];
}
}
}
-(无效)从视图中移除ClearButton:(UIView*)视图
{
如果(!视图)
{
返回;
}
用于(UIView*视图中的子视图。子视图)
{
[self-removeClearButtonFromView:子视图];
}
if([查看一致性协议:@协议(UIT)])
{
UITextField*文本视图=(UITextField*)视图;
if([textView respondsToSelector:@selector(setClearButtonMode:)]))
{
[textView setClearButtonMode:UITextFieldViewModeNever];
}
}
}
在iOS7中执行此操作的更好方法是:
[[UITextField appearance when contained in:[UISearchBar class],nil]setClearButtonMode:UITextFieldViewModeWhileEditing];
她的是我写的一个这样做的类别
类别
@implementation UISearchBar (Additions)
- (void)setClearButtonMode:(UITextFieldViewMode)viewMode {
UITextField *textField = [self findTextFieldInView:self];
[textField setClearButtonMode:viewMode];
}
- (UITextField *)findTextFieldInView:(UIView *)view {
for (UIView *subview in view.subviews) {
if ([subview isKindOfClass:[UITextField class]] ||
[subview.class isSubclassOfClass:[UITextField class]]) {
return (UITextField *)subview;
}
UITextField *textField = [self findTextFieldInView:subview];
if (textField) {
return textField;
}
}
return nil;
}
@end
用法
您需要获取搜索栏的文本字段
UITextField *textField = [searchBar valueForKey:@"_searchField"];
textField.clearButtonMode = UITextFieldViewModeNever;
在-(void)searchBartextdibeginediting:(UISearchBar*)searchBar方法中使用
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
UITextField *textField = [searchBar valueForKey:@"_searchField"];
textField.clearButtonMode = UITextFieldViewModeNever;
}
这里有一种比任何答案都好的方法,而且您不必使用私有API或遍历子视图来实现这一点
UISearchBar有一个用于执行此操作的内置API:
您想要的搜索栏图标键是UISearchBaricClear,并且您想要uicontrol状态正常状态。然后为该图像提供一个清晰的图像,您就完成了
所以,它应该是这样的:
[searchBar setImage:clearImage forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
用于搜索栏中的(x)图标。您可以使用下面的委托方法
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = YES;
}
接受的答案在iOS7+上不起作用,下面是作为Swift扩展的修改版本
extension UIView {
class func removeClearButton(svs: [UIView]) {
for sv in svs {
if let tv = sv as? UITextField where sv.conformsToProtocol(UITextInputTraits) {
tv.clearButtonMode = .Never
return
} else {
UIView.removeClearButton(sv.subviews)
}
}
}
}
用法
Jasarien干杯,这似乎正是我所需要的。不幸的是,我在[子视图conformsToProtocol:UITestePuttraits]
中遇到了一个错误,它的内容是:在'UITestePuttraits'之前需要表达式。
。我需要导入或符合任何内容吗?我的错!抱歉。您需要执行[子视图conformsToProtocol:@protocol(uitPuttraits)]
就是这样!你真是个天才。谢谢你的朋友。顺便问一下,你有没有用一个应用程序在你的屏幕截图上做那个小注释(啊!->)?如果有,是什么应用程序?看起来很有用:)不幸的是,这只是一个Photoshop画笔工具的工作。虽然一个注释应用程序是个好主意!这可以很容易地在UISearchBar上成为一个类别,以实现重用。似乎会导致崩溃,显然有多个视图符合uitExtraits
,并且该视图无法响应setClearButtonMode
防止崩溃我将其更改为:for(UIView*子视图在[self.searchDisplayController.searchBar子视图]{[self-configuresearchbar视图:子视图];}
后跟配置searchBar视图:(UIView*)视图
方法。在我的情况下,似乎是搜索栏本身导致了崩溃。最佳解决方案!谢谢。
for (UIView *subview in _search_bar.subviews)
{
NSLog(@"%@",subview.subviews);
for (UIView *subview11 in subview.subviews)
{
if ([subview11 conformsToProtocol:@protocol(UITextInputTraits)])
{
[(UITextField *)subview11 setClearButtonMode:UITextFieldViewModeNever];
}
}
}
extension UIView {
class func removeClearButton(svs: [UIView]) {
for sv in svs {
if let tv = sv as? UITextField where sv.conformsToProtocol(UITextInputTraits) {
tv.clearButtonMode = .Never
return
} else {
UIView.removeClearButton(sv.subviews)
}
}
}
}
UIView.removeClearButton(searchBar.subviews)