Ios 带有UISearchDisplayController的UISearchBar在屏幕外设置动画

Ios 带有UISearchDisplayController的UISearchBar在屏幕外设置动画,ios,ipad,uisearchbar,uisearchdisplaycontroller,Ios,Ipad,Uisearchbar,Uisearchdisplaycontroller,我有标准的iPad视图控制器,顶部有一个自定义导航栏。在xib文件中,我添加了一个与视图右边缘对齐的UISearchBar。搜索栏的宽度为320px。我初始化了一个searchdisplaycontroller,如下所示: // Search display controller self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar

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

// Search display controller
self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar 
                                                                                contentsController:self];
_mySearchDisplayController.delegate = self;
_mySearchDisplayController.searchResultsDataSource = self;
_mySearchDisplayController.searchResultsDelegate = self;
- (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];
}
问题是,当我按下搜索栏时,该栏的大小调整为整个视图的全宽,但保持其x位置。这意味着它延伸到屏幕之外很远。我猜这与在搜索栏旁边滑入的“取消”按钮有关。如果我将搜索栏放在屏幕的最左侧,它将动画显示到屏幕的整个宽度,并且可以看到“取消”按钮


任何人都有解决方案吗?

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

// Search display controller
self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar 
                                                                                contentsController:self];
_mySearchDisplayController.delegate = self;
_mySearchDisplayController.searchResultsDataSource = self;
_mySearchDisplayController.searchResultsDelegate = self;
- (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];
                     }];
}
我已经回答了一个类似的问题,你可以检查一下,我也放了一些图片

你唯一要做的就是修改iPad的代码