带有UISearchDisplayController的UISearchBar在屏幕外设置动画 [英] UISearchBar with UISearchDisplayController animates outside screen

查看:73
本文介绍了带有UISearchDisplayController的UISearchBar在屏幕外设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有标准的iPad视图控制器,顶部有一个自定义导航栏。在xib文件中,我添加了一个与视图右边缘对齐的UISearchBar。搜索栏的宽度为320像素。我这样初始化一个searchdisplaycontroller:

I have standard iPad view controller which has a custom navigation bar at the top. In the xib-file I've added a UISearchBar aligned to the right edge of the view. The search bar is 320px in width. I init a searchdisplaycontroller like this:

// Search display controller
self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar 
                                                                                contentsController:self];
_mySearchDisplayController.delegate = self;
_mySearchDisplayController.searchResultsDataSource = self;
_mySearchDisplayController.searchResultsDelegate = self;

问题是,当我按下搜索栏时,栏会调整大小为整个视图,但保持其x位置。这意味着它延伸到屏幕外。我猜它与取消按钮有关,该按钮在搜索栏旁边滑动。如果我将搜索栏放在屏幕的最左侧,它会动画到屏幕的整个宽度,并且取消按钮可见。

The problem is that when I press the search bar, the bar resizes to be the full width of the entire view, but keeps its x-position. This means that it stretches far outside the screen. I'm guessing it has something to do with the "Cancel" button that slides in next to a search bar. If I place the search bar to the far left in the screen, it animates to the full width of the screen and the cancel button is visible.

任何人都有解决方案这个?

Anyone has a solution for this?

推荐答案

你可以在 UISearchBar 的框架中设置动画 searchDisplayControllerWillBeginSearch 更正其位置的方法如下:

You can animate the frame of your UISearchBar in the searchDisplayControllerWillBeginSearch method to correct its position like this:

- (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 if you need to
    [self.toolbar setItems:nil animated:YES];
}

并在完成搜索后再次为其设置动画:

and the animate it back again when finished searching:

- (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 arrayWithObjects: /* put your bar button items here */] animated:YES];
                     }];
}

我已经回答了类似的问题,你可以查一下 ,我也放了一些图片。

I have answered a similar question, you can check it here, I've put some images too.

你唯一要做的就是做的是调整iPad的代码。

The only thing you have to do is to adapt the code for the iPad.

这篇关于带有UISearchDisplayController的UISearchBar在屏幕外设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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