iOS - 使用UISearchDisplayController和UISearchBar,它是UIToolbar中的UIBarButtonItem [英] iOS - using UISearchDisplayController with UISearchBar that is UIBarButtonItem in UIToolbar

查看:135
本文介绍了iOS - 使用UISearchDisplayController和UISearchBar,它是UIToolbar中的UIBarButtonItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人尝试过在UIToolbar中使用UISearchBar作为UIBeButtonItem的UISearchDisplayController?

Has anyone tried using a UISearchDisplayController with a UISearchBar that is a UIBarButtonItem in a UIToolbar?

我很欣赏如何成功完成此操作的提示。

I would appreciate tips on how to do this successfully.

目前,当搜索控制器弹出时,它会重绘UISearchBar,我很难保持与UIToolbar类似的外观

Currently whenever the search controller pops up it redraws the UISearchBar and I'm struggling to maintain a similar look to the UIToolbar

推荐答案

通常你不想在工具栏中放置一个搜索栏,但是,你似乎想要做类似于我所做的事情。

Usually you don't want to put a search bar inside a toolbar, however, it seems you want to do something similar to what I did.

所以这就是我如何做到的,它可能被称为黑客,但它就像一个魅力:)

So here is how I did it, it may be called a hack, but it works like a charm :)

首先你需要设置它在这样的界面生成器中:

First you have to set it up in interface builder like this:

请注意,搜索不是工具栏的子项,而是在上面。

Notice that the search is not a child of toolbar, instead it is above.

搜索栏应具有清晰的颜色背景和灵活的左,右和宽度a ugesize mask。

The search bar should have "clear color" background and flexible left, right and width autoresizing masks.

你在工具栏下面放了一个带有黑色背景的1像素标签,即。 [x = 0,y = 44,宽度= 320或帧宽,高度= 1],还有灵活的左,右和宽度自动调整遮罩。这是在搜索显示控制器显示表格后隐藏您获得的一个可见像素视图。尝试不用它来理解我的意思。

You put a 1-pixel label with black background below the toolbar, ie. [x=0, y=44, width=320 or frame width, height=1], also flexible left, right and width autoresizing masks.This is to hide the one visible pixel you get, after the search display controller shows the table view. Try it without it to understand what I mean.

你设置任何工具栏项目,并确保有他们的出口,因为你将需要那些。

You setup any tool bar items and be sure to have outlets for them, since you will be needing those.

现在代码...

当你开始搜索时:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
    // animate the search bar to the left ie. x=0
    [UIView animateWithDuration:0.25f animations:^{
        CGRect frame = controller.searchBar.frame;
        frame.origin.x = 0;
        controller.searchBar.frame = frame;
    }];
    // remove all toolbar items
    [self.toolbar setItems:nil animated:YES];
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    // animate search bar back to its previous position and size
    // in my case it was x=55, y=1
    // and reduce its width by the amount moved, again 55px
    [UIView animateWithDuration:0.25f 
                          delay:0.0f
                        // the UIViewAnimationOptionLayoutSubviews is IMPORTANT,
                        // otherwise you get no animation
                        // but some kind of snap-back movement 
                        options:UIViewAnimationOptionLayoutSubviews 
                     animations:^{
                         CGRect frame = self.toolbar.frame;
                         frame.origin.y = 1;
                         frame.origin.x = 55;
                         frame.size.width -= 55;
                         controller.searchBar.frame = frame;
                     } 
                     completion:^(BOOL finished){
                         // when finished, insert any tool bar items you had
                         [self.toolbar setItems:[NSArray arrayWithObject:self.currentLocationButton] animated:YES];
                     }];
}

有了这个,我得到了以下动画效果:)

With this I get the following with a nice animation :)

这篇关于iOS - 使用UISearchDisplayController和UISearchBar,它是UIToolbar中的UIBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆