ios - UISearchBar 与 UISearchDisplayController 在屏幕外动画

标签 ios ipad uisearchbar uisearchdisplaycontroller

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

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

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

谁有解决办法吗?

最佳答案

您可以在 searchDisplayControllerWillBeginSearch 方法中对 UISearchBar 的框架进行动画处理,以更正其位置,如下所示:

- (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];
}

完成搜索后再次将其动画化:

- (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];
                     }];
}

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

您唯一需要做的就是针对 iPad 调整代码。

关于ios - UISearchBar 与 UISearchDisplayController 在屏幕外动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11579646/

相关文章:

ios - 在应用程序从后台返回之前,在 applicationDidEnterBackground 中添加的 View 不可见

html - 是否存在用于在本地测试网站的 Ipad 模拟器

json - 如何实现 TableView 中显示的json的搜索操作

ios - UISearchController searchBar 在第一次点击时消失

ios - 在 iOS5 问题中保存数据

ios - "Missing load commands"上传具有自己的静态框架和包的应用程序时的电子邮件

javascript - 有没有一种方法可以让网站确定它是否正在 iPad 上查看以及是否正在使用 iPhone/Android/Pre 查看

iphone - iOS - QLPreviewController - 如何阻止 QuickLook 旋转?

android - 如何让 Action Bar SearchView 填满整个 action bar?

ios - SpriteKit 碰撞检测中 SKSpriteNodes 之间的间隙