我有一个带有UISearchBar的tableView。升级到iOS 4之前,当用户选择一个单元格并按下新的viewController时,键盘会滑到桌子左侧的视图外。
现在,它只是停留在新视图顶部的屏幕上。如果我调用[mySearchBar resignFirstResponder];我可以摆脱键盘,但是动画很尴尬,因为在按下新视图时它滑离了屏幕底部。
有谁知道为什么iOS 4中的行为会有所不同?我如何才能将键盘绑定到tableView,以便在按下新视图时将其滑出屏幕?
谢谢!
编辑:
这是一些其他信息。我在导航栏后面有uisearchbar。当用户按下按钮时,它会滑出。我像这样添加uisearchbar:
[self.navigationController.view insertSubview:self.mySearchBar belowSubview:self.navigationController.navigationBar];
我猜是因为导航栏实际上并没有被推到下一个视图(尽管搜索栏确实有),所以键盘保持在原位。
我尝试在表视图后面创建一个uiview并将搜索栏添加到其中,但是由于UINavigationController和我的代码结构,我无法使其正常工作。
最佳答案
就个人而言,我建议实现一个使用UISearchDisplayController的SearchBar。我认为这实现了xCode所使用的标准模板,并能真正使所有响应者顺利处理。这是一个示例方法,您可以尝试将其复制到代码中并使用
[self addSearchBar:@"enter search keywords here" hasScope:NO]
推荐代码:
- (void) addSearchBar:(NSString*)placeholder hasScope:(BOOL)aScope {
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[searchBar sizeToFit];
//searchBar.delegate = self;
searchBar.placeholder = placeholder;
self.tableView.tableHeaderView = searchBar;
if (aScope==YES){
searchBar.showsScopeBar = YES;
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"Flags",@"Listeners",@"Stations",nil];
}
UISearchDisplayController *searchDC = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
// The above assigns self.searchDisplayController, but without retaining.
// Force the read-only property to be set and retained.
[self performSelector:@selector(setSearchDisplayController:) withObject:searchDC];
//searchDC.delegate = self;
//searchDC.searchResultsDataSource = self;
//searchDC.searchResultsDelegate = self;
[searchBar release];
[searchDC release];
}
关于iphone - 在iOS 4中选择单元格时,UISearchBar键盘不会滑出 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3219339/