【发布时间】: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 会比手动调整视图大小容易得多。
标签: iphone ios uitableview uiedgeinsets