如何使用标题视图和UITableview在自定义视图中正确动画UISearchbar?

Ali*_*Ali 0 iphone objective-c uitableview uisearchbar ios

我有一个自定义的UIViewController,它来自一个具有以下视图层次结构的nib文件:

笔尖

...在左侧橙色区域是一个"标题视图",中间有两个UIButton和一个UILabel,蓝色区域是一个UITableView; 所有在一个UIView下.

在运行时,这将呈现或多或少如下:

给予

此时,每当用户点击搜索栏时,我都希望隐藏导航栏和标题视图,并将搜索栏移动到窗口顶部.

但事实并非如此,因为导航栏会自动退出窗口并且标题视图将移至顶部,但仍会保留在场景中; 休息会坚持原来的地方.

此时我必须添加以下代码才能将标题视图移出窗口:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];

    CGRect frame = self.headerView.frame;
    frame.origin.y -= frame.size.height;
    self.headerView.frame = frame;

    [UIView commitAnimations];
}
Run Code Online (Sandbox Code Playgroud)

这将导致:

搜索

...您可能已经注意到标题视图将移动得很好,但搜索栏将保持其原始位置.

我试图重新定位两个self.view或搜索表视图,但没有运气.

我该怎么办?谢谢.

Ali*_*Ali 8

请确保您实现searchDisplayControllerWillBeginSearch:searchDisplayControllerWillEndSearch:UISearchDisplayDelegate,并使用类似的东西:

[UIView beginAnimations:nil context:NULL];

CGRect headerViewFrame = self.headerView.frame;
headerViewFrame.origin.y -= 44.0f;
self.headerView.frame = headerViewFrame;

CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.origin.y -= 44.0f;
self.tableView.frame = tableViewFrame;

[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

[UIView beginAnimations:nil context:NULL];

CGRect headerViewFrame = self.headerView.frame;
headerViewFrame.origin.y += 44.0f;
self.headerView.frame = headerViewFrame;

CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.origin.y += 44.0f;
self.tableView.frame = tableViewFrame;

[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

......因此.

  • 如果您首先找到解决方案,那么发布解决方案总是好的. (3认同)