向下滑动搜索栏并同时调整同级视图的大小

Posted

技术标签:

【中文标题】向下滑动搜索栏并同时调整同级视图的大小【英文标题】:Sliding Search Bar Down and Simultaneously Resizing Sibling View 【发布时间】:2012-06-19 18:24:58 【问题描述】:

我有一个UIView——称之为HomeView,其中包含一个UITableView和一个UISearchBar。也就是说,它们是子视图。这些是在viewDidLoad 中为HomeView 添加的。一图胜千言:

从这些屏幕截图可以看出,UITableView 中的第一项在 UISearchBar 显示后被隐藏。以下是相关代码:

- (void) viewDidLoad
  self.table_view = [UITableView.alloc.initWithFrame: self.view.bounds style:UITableViewStylePlain];
  self.table_view.bounds.origin.y += 44;
  self.table_view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  // ^^^ This is the code I believe is one source of my problem



// Triggered when search button in nav bar is pressed

// code to calculate delta here

// Here's how the view is animated. The search bar frame
// is dropped down (works), and ideally, the tableview
// drops down exactly the same amount at the same time.
// If the action is to hide, then the inverse happens
// because +delta+ will have been calculated as a
// negative number above.
[UIView animateWithDuration: 0.7
  delay: 0.0
  options: UIViewAnimationOptionCurveEaseIn
  animations:^ 
    self.searchBar.frame = CGRectOffset(@searchBar.frame, 0.0, delta);
  
  completion:^(BOOL finished)
    self.searchBarVisible = !self.searchBarVisible
    self.searchBar.hidden = !self.searchBarVisible
    edgeInsets = UIEdgeInsetsMake(0.0f, self.searchBarVisible ? delta : 0.0f, 0.0f, 0.0f)
    [tableView setContentInset: edgeInsets]
  
];

实际发生的情况是,搜索栏在按下搜索按钮时按预期向下滑动,然后在下一次按下按钮时向上滑动。但是,表格视图永远不会调整。

据我所知,问题是在viewDidLoad 中,封闭的HomeView 还没有被渲染,所以大小是未知的。我可以获取屏幕尺寸并使用它,但我认为自动调整大小蒙版是更好的做法,对吧?

这是否有明显的缺陷,有没有办法让表格视图在搜索栏同时向下动画?

谢谢

【问题讨论】:

我讨厌成为那种人,但为什么不直接使用 tableViewController 呢?这样做然后关注this piece of apple sample code 会比手动调整视图大小容易得多。 【参考方案1】:

好的,让我们再试一次!

在您的动画块中,将您制作 edgeInsets 的行更改为:

edgeInsets = UIEdgeInsetsMake(self.searchBarVisible ? delta : 0.0f, 0.0f, 0.0f, 0.0f);

据我所知,第一个参数是顶部,而您设置的参数是左侧插入。

【讨论】:

这非常有效。位置参数真的把我搞砸了。

以上是关于向下滑动搜索栏并同时调整同级视图的大小的主要内容,如果未能解决你的问题,请参考以下文章

Twitter引导导航栏和下拉菜单-不调整大小

UISearchBar 在解雇后正在调整大小

启用自动布局的 iOS 调整视图大小

Lion 全屏菜单栏不向下滑动

UITableView 标题视图调整大小

滑动后调整超级视图大小时,UIPageViewController 视图会跳转