简体   繁体   English

如何在iOS中模仿UISearchBar的Safari行为

[英]How to mimic UISearchBar's behaviours of Safari in iOS

I have an UISearchBar in the titleView of navigation bar (it looks like the picture below) and I want make it works like the one in Safari browser. 我在导航栏的titleView中有一个UISearchBar (看起来如下图所示),我想让它像Safari浏览器中的那样工作。

I don't have enough reputation to post images, here's the link 我没有足够的声誉来发布图片,这是链接

First picture 第一张照片

在此输入图像描述

Second picture 第二张照片

在此输入图像描述

What I want to achieve are: 我想要实现的目标是:

  • In normal state, the Navigation Bar contains 2 left and right buttons, an UISearchBar and a clear button inside that search bar (looks like first picture with 2 addition buttons). 在正常状态下,导航栏包含2个左右按钮, UISearchBar和搜索栏内的清除按钮(看起来像带有2个添加按钮的第一张图片)。
  • In search state, the view is replaced by another view and when it dismissed, the view back to original state. 在搜索状态中, view被另一个view替换,当它被解除时, view返回到原始状态。
  • Works both in iOS 6 and 7 适用于iOS 6和7

I know this can be done by using UISearchDisplayController but it doesn't work. 我知道这可以通过使用UISearchDisplayController来完成,但它不起作用。 Here's my code: 这是我的代码:

.h : implement TableView's DataSource/Delegate, UISearchDisplayDelegate, UISearchBarDelegate .h :实现TableView's DataSource/Delegate, UISearchDisplayDelegate, UISearchBarDelegate

.m .M

-(void) viewDidload
{
    //Add left, right buttons
    self.leftButton = [[UIBarButtonItem alloc] init];
    [self.leftButton setStyle:UIBarButtonItemStylePlain];
    [self.leftButton setTitle:@"Button"];
    self.navigationItem.leftBarButtonItem = self.leftButton;

    self.rightButton = [[UIBarButtonItem alloc] init];
    [self.rightButton setStyle:UIBarButtonItemStylePlain];
    [self.rightButton setTitle:@"Button"];

    self.navigationItem.rightBarButtonItem = self.rightButton;
    self.searchBar = [[UISearchBar alloc] initWithFrame:self.navigationController.navigationBar.bounds];
    if (DEVICE_IS_IOS7) {
        self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    }
    self.navigationItem.titleView = self.searchBar;
    self.searchBar.delegate = self;
    //----------------

    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
    self.searchController.delegate = self;
    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;
}

    /*
    table implementation
    */

In iOS 7, the view of UISearchDisplayController (the grey one) didn't cover the main view and resultTable did not reload data, even it was able to filter the search string. 在IOS 7中, viewUISearchDisplayController (灰色之一)并不包括主视图和resultTable没有重新加载数据,即使它能够过滤搜索字符串。 (I had to use KVO to display it) (我不得不用KVO来显示它)

In iOS 6, the grey thing covers all the screen and keyboard didn't show up (it disappeared immediately). 在iOS 6中,灰色的东西覆盖了所有的屏幕和键盘没有显示(它立即消失)。

Can anybody help ? 有人可以帮忙吗? Thank you. 谢谢。

I've twice had to do a very similar UI to what you're describing, the recent one being closer to Twitter for iOS 7's UISearchBar behavior. 我已经两次不得不做一个与你所描述的非常相似的UI,最近的一个更接近Twitter的iOS 7的UISearchBar行为。 If you want to display a UISearchBar inside a UINavigationBar , forget that setup altogether because you will likely encounter roadblocks, like this (although it seems fixed by Apple ). 如果你想显示UISearchBar一个内部UINavigationBar ,忘了设置完全是因为你可能会遇到的障碍,像这样 (虽然它似乎被苹果定 )。

Fake it instead. 相反假。 Hide the navigation bar in your UIViewController and use a UIView with a height of 64 for iOS 7 or 44 for iOS 6 and put it on top, so that it looks like a navigation bar. 隐藏UIViewController的导航栏,并使用UIView ,其高度为64适用于iOS 7)或44适用于iOS 6)并将其置于顶部,以使其看起来像导航栏。 Add the UISearchBar inside it like a normal subview, then animate it as you wish in the UISearchBarDelegate methods. 像普通子视图一样在其中添加UISearchBar ,然后在UISearchBarDelegate方法中根据需要设置动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM