搜索栏子视图不起作用,搜索栏未调用 UITextField 委托方法
Posted
技术标签:
【中文标题】搜索栏子视图不起作用,搜索栏未调用 UITextField 委托方法【英文标题】:Search bar subviews not working and UITextField delegate methods not called for searchbar 【发布时间】:2013-12-19 11:18:46 【问题描述】:请看我下面的代码:
我的 viewController 中有一个搜索栏
for (UIView *subView in self.searchBar.subviews)
if([subView conformsToProtocol:@protocol(UITextInputTraits)])
[(UITextField *)subView setDelegate:self];
[(UITextField *)subView setReturnKeyType:UIReturnKeyDefault];
[(UITextField *)subView setBackground:[UIImage imageNamed:@"shopping-Search"]];
for (UIView *subview in self.searchBar.subviews)
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
[subview removeFromSuperview];
break;
此代码在 ios 6 中运行良好,但在 iOS 7 中无法运行。
【问题讨论】:
【参考方案1】:iOS7 中的 searchBar 子视图层次结构已更改,请尝试以下操作:
iOS7:
NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
iOS6 及之前:
NSArray *searchBarSubViews = self.searchBar.subviews;
试试下面的更新功能:
for (UIView *subView in searchBarSubViews)
if([subView conformsToProtocol:@protocol(UITextInputTraits)])
[(UITextField *)subView setDelegate:self];
[(UITextField *)subView setReturnKeyType:UIReturnKeyDefault];
[(UITextField *)subView setBackground:[UIImage imageNamed:@"shopping-Search"]];
for (UIView *subview in searchBarSubViews)
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
[subview removeFromSuperview];
break;
【讨论】:
以上是关于搜索栏子视图不起作用,搜索栏未调用 UITextField 委托方法的主要内容,如果未能解决你的问题,请参考以下文章