ios - 具有 map 应用程序行为的搜索栏(选择时导航栏全宽)

标签 ios iphone uinavigationbar uisearchbar uisearchdisplaycontroller

我正在尝试显示一个搜索栏,它的行为与 native map 应用中的搜索栏完全相同。我的意思是:

  • 我的导航栏标题 View 中的搜索栏
  • 选中后,搜索栏占据我的导航栏的整个宽度并且我显示 SearchDisplayController

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

enter image description here

正如您在上面看到的,我无法让搜索栏在选择时占据整个宽度。虽然,全宽 + 取消按钮似乎是与 SearchDisplayController Hook 的搜索栏的默认行为,至少如果搜索栏未添加到导航栏!

我是否缺少一种显而易见的方法?还是调用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;
}

此外,我想知道 Maps 应用程序是否真的使用了导航 Controller ,因为有些自定义行为(titleView 的宽度、leftBarButton 离开屏幕的动画)让我感到惊讶。

关于ios - 具有 map 应用程序行为的搜索栏(选择时导航栏全宽),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19773997/

相关文章:

iOS - 使用 Swift 的 CAEmitterCell

ios - 调用 `[UISearchBar setReturnKeyType:]` 时无法识别的选择器

ios - 缩放图像返回 1 :1 if UIButton image is changed

iphone - iphone 中的 UILabel 字体问题

iphone - 将 MKPolygon 作为覆盖层添加到 MKMapView 时出现问题

iphone - iOS - 在动画过渡期间更新标签?

swift - 当 UISearchController 聚焦时重置 UIView

ios - 核心数据一对一单向关系

ios - swift2.3 中的推送 View Controller 中导航栏颜色变为黑色

iphone - iOS:带有按钮图像的导航栏