ios – 具有地图应用行为的搜索栏(选中时导航栏全宽)

我正在尝试显示一个搜索栏,其行为与原生地图应用中的行为完全相同.我的意思是:

>我的导航栏标题视图中的搜索
>选中后,搜索栏占据导航栏的整个宽度,然后显示SearchdisplayController

到目前为止,我设法得到以下行为:

正如您在上面所看到的,我无法让搜索栏占据选择的全宽.虽然,全宽取消按钮似乎是与SearchdisplayController挂钩的搜索栏的认行为,至少如果搜索栏没有添加到导航栏!

我错过了一种明显的方法吗?或者,当调用searchBarShouldBeginEditing时,我是否必须自己自定义导航栏?

解决方法

只要我没有完美的解决方案,我就会做以下事情.但我仍然愿意接受更好的事情!
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onCancel)];
    [UIView animateWithDuration:0.1 animations:^(){
        self.navigationItem.leftBarButtonItem = nil;
    }];
return YES;
}

- (void)onCancel {
    [self.searchController setActive:NO];
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    [UIView animateWithDuration:0.1 animations:^(){
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"OpenMenuButton.png"] style:UIBarButtonItemStylePlain target:self action:@selector(openSideMenu:)];
    }];
    self.navigationItem.rightBarButtonItem =self.doneButton;
    return YES;
}

此外,我想知道地图应用程序是否实际使用导航控制器,因为有令我感到惊讶的自定义行为(titleView的宽度,leftBarButton的动画在屏幕外).

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...