【发布时间】:2016-07-01 15:53:16
【问题描述】:
我有 UISearchBar,当我单击 UISearchBar 中的搜索按钮时,我想显示另一个 UIViewController。我正在使用以下方法:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main"
bundle:nil];
UIViewController *add =
[storyboard instantiateViewControllerWithIdentifier:@"mainMenuViewController"];
[self.navigationController pushViewController:add animated:YES];
}
问题是 UIViewController 的出现动画被调用了两次。我看到旧控制器从右侧向左滑动两次。
现在,如果我在单击某些常规 UIButton 时使用相同的代码显示新控制器,动画问题就消失了。所以它似乎与 UISearchBar 有某种关系。
searchBarSearchButtonClicked 的委托方法只调用一次。
重现问题的代码非常少,可能我稍后会附上示例项目。
但是带有搜索栏的 UIVIewCOntroller 看起来像这样:
- (void)viewDidLoad {
[super viewDidLoad];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
searchBar.placeholder = NSLocalizedString(@"search a pub or a place", nil);
searchBar.delegate = self;
[self.navigationItem setTitleView:searchBar];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
DLog(@"Filter searchBarSearchButtonClicked");
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main"
bundle:nil];
UIViewController *add =
[storyboard instantiateViewControllerWithIdentifier:@"mainMenuViewController"];
[self.navigationController pushViewController:add animated:YES];
}
iOS 8 和 9 都存在问题。
编辑:
我上传了示例项目here。它非常简约。
编辑2:
如果我这样呈现控制器,而不是通过导航控制器,动画只会显示一次。 (虽然这是另一个动画 - 从下往上,前一个是从右到左)。但我需要通过导航控制器来完成。所以问题似乎与 UISearchBar Search clicked + Navigation Controller 有关。
[self presentViewController:add
animated:YES
completion:nil];
当然我也可以这样不带动画的调用:
[self.navigationController pushViewController:add animated:NO];
但是如果我想要动画怎么办。
编辑3:
我尝试通过 segue 显示第二个控制器,但没有帮助。
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self performSegueWithIdentifier:@"seg1" sender:self];
}
编辑4:
我已经向 Apple 报告了这个错误,因为我猜这一定是 UIKit 中的错误。无论如何,如果有人能找到解决方法,那就太好了。
【问题讨论】:
-
也许有些事情与 UISearchController 的解雇混淆了。要尝试的一件事是将您的按钮委托代码移动到具有简单签名的方法中,然后使用
self performSelector调用它。 -
感谢您的想法,我已经尝试过了,但问题仍然存在。
标签: objective-c animation uiviewcontroller uinavigationcontroller uisearchbar