IOS UITableView contentOffSet在从推送的详细视图返回时不再隐藏标头

| 我在根视图控制器中使用下面的代码隐藏UITableView的标题(标题中有一个UISearchbar)。当应用程序启动并显示tableView时,它将起作用。但是,随后,当选择一行时,将推入详细视图,并且用户弹出详细视图,则uitableview标头现在在根视图中可见,而我所期望的。 以下是相关功能:
- (void) viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
    self.tableView.contentOffset 
     = CGPointMake(0, self.tableView.tableHeaderView.frame.size.height);
    //it\'s as if the line above is ignored on when returning from a pushed detail view    
}

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // theContentOffSet works when I put it hear, but then the user can see it which is not desired    
}
行''[self.navigationController setNavigationBarHidden:YES animation:animated]; \'当然是问题的一部分,因为没有它,代码将起作用,并且tableView标头会滚动到视图之外。但是,对于根视图的要求是要隐藏导航栏,但要在详细视图中显示。     
已邀请:
环顾了一会后,我发现了以下帖子https://devforums.apple.com/message/315519#315519解决了这个问题。
-(void)viewWillAppear:(BOOL)animated
{
    [self performSelector:@selector(updateContentOffset) withObject:nil afterDelay:0.0];
}

- (void)updateContentOffset
{
    self.tableView.contentOffset = CGPointMake(0, savedContentOffsetY);
}
当然,在
viewWillDisappear
中,您可以按如下方式保存内容偏移量:
savedContentOffsetY = self.tableView.contentOffset.y;
在你的
viewDidLoad
savedContentOffsetY = self.tableView.tableHeaderView.frame.size.height;
    
我进行了搜索,尝试了很多,但没有帮助。最后,以下代码帮助了我。您可以在viewDidLoad()方法中添加代码:
self.edgesForExtendedLayout = UIRectEdgeNone;
    

要回复问题请先登录注册