滑动搜索栏并同时调整兄弟视图的大小

时间:2012-06-19 18:24:59

标签: iphone ios uitableview uiedgeinsets

我有一个UIView - 称之为HomeView,其中包含UITableViewUISearchBar。也就是说,它们是子视图。 viewDidLoadHomeView添加了这些内容。一张图片胜过千言万语:

enter image description here

enter image description here

从这些屏幕截图中可以看出,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尚未呈现,因此大小尚未知。我可以获得屏幕尺寸并使用它,但我认为自动调整大小的面具是更好的练习,对吧?

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

由于

1 个答案:

答案 0 :(得分:0)

好的,让我们再试一次!

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

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

据我所知,第一个参数是顶部,你设置的是左边插图。