2015-12-07 76 views
0

這裏是我的UIView明細:頂部問題與iOS上的UITableView滾動在我的表頭是不是滾動

  • UIView。我將它用作容器視圖,其中包含其他對象。
  • UITableView低於容器視圖。

我想要滾動的UITableView開始通過容器視圖,然後滾動表中的內容。

我希望我的桌子有一個標題視圖,該視圖將堅持在我的UINavigationController下的頂部。

如果我做了類似myTableView.tableHeaderView的事情,它會在導航欄下滾動。

如果我使用表視圖方法添加標題視圖,它會凍結在屏幕中間(它開始的位置)。

這裏是我的viewDidLayoutSubviews代碼:

- (void)viewDidLayoutSubviews { 
    [super viewDidLayoutSubviews]; 
    [self.myTableView setContentInset:UIEdgeInsetsMake(self.containerView.bounds.size.height, 0.f, 0.f, 0.f)]; 
    [self.myTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO]; 

    float headerImageYOffset = 88 + self.containerView.bounds.size.height - self.view.bounds.size.height; 
    CGRect headerImageFrame = self.containerView.frame; 
    headerImageFrame.origin.y = headerImageYOffset; 
} 

這裏是我的頭碼和滾動碼:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    // This is made in Interface Builder 
    return self.headerView; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    return 50; 
} 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 
    CGFloat scrollOffset = -scrollView.contentOffset.y; 
    CGFloat yPos = scrollOffset - _containerView.bounds.size.height; 
    _containerView.frame = CGRectMake(0, yPos, _containerView.frame.size.width, _containerView.frame.size.height); 
} 

有什麼需要改進,以使頭部與滾動表並留導航下頂部的酒吧?

如果需要查看其他代碼,請告訴我。

編輯:

我增加了更多的代碼,用什麼,我試圖完成去。往上看。

回答

0

你並不需要實現scrollViewDidScroll:,只需添加表視圖的父和頭部之間的自動佈局約束:

- (void)viewDidLoad { 
    NSLayoutConstraint* contraint = [NSLayoutConstraint constraintWithItem:self.headerView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:self.topLayoutGuide.length]; 
    [self.view addConstraint:contraint]; 
} 
+0

刪除我的scrollViewDidScroll方法帶走了我要去的,雖然效果。 –