【问题标题】:How to add a view on top of UITableView that scrolls together, but stick to top after scrolling如何在 UITableView 顶部添加一个一起滚动的视图,但在滚动后粘在顶部
【发布时间】:2017-04-04 16:17:29
【问题描述】:

我的 UITableView 顶部有一个高度为 100 像素的 UIView。当我向上滚动时,我希望 UIView 与我的 UITableView 一起滚动,就好像它是它的一部分一样。当我的 UIView 的 50 像素被隐藏而无法向上滚动时,我想在继续向上滚动的同时将 UIView 固定在顶部。如何做到这一点?我尝试使用将 UIView 的顶部 NSLayoutConstraint 常量更改为等于我的 tableview 的内容偏移量,但它们不会以相同的速度滚动。

【问题讨论】:

标签: ios swift uitableview uiscrollview


【解决方案1】:

首先确保你的 tableView 是grouped:

self.tableView = UITableView(frame: CGRect(x: 0, y: (self.navigationController?.navigationBar.frame.maxY)!, width: self.view.bounds.size.width, height: (self.view.bounds.size.height - (self.navigationController?.navigationBar.frame.maxY)!)), style: .grouped)
self.tableView.delegate = self
self.tableView.dataSource = self
self.view.addSubview(self.tableView)  

然后您需要将UIView 添加到您的tableView 的subview 中:

self.myView = UIView()
self.myView.backgroundColor = UIColor.green
self.myView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 100)
self.tableView.addSubview(self.myView)

为您的 tableView 的第一部分添加一个高度为 100 的标题,以便您的单元格位于正确的位置:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 100
}

然后在滚动时调整UIView的框架:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offset = scrollView.contentOffset.y
    if(offset > 50){
        self.myView.frame = CGRect(x: 0, y: offset - 50, width: self.view.bounds.size.width, height: 100)
    }else{
        self.myView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 100)
    }
}

演示:

【讨论】:

  • 这是一种绝妙的方法,也是我找到的最佳答案。这应该是公认的答案。
【解决方案2】:

如果我正确理解您的问题,您可以通过像这样实现表视图scrollViewDidScroll: 方法来做到这一点

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    static CGFloat previousOffset;
    CGRect rect = self.yourUIView.frame;

   //NSLog you'r UIView position before putting any condition 
   // NSLog(@"Origin %f",rect.origin.y);

    rect.origin.y += previousOffset - scrollView.contentOffset.y;
    previousOffset = scrollView.contentOffset.y;

    //assuming you'r UIView y position starts from 0
    //Before setting the condition please make sure to run without any 
    //condition if not working

    if(rect.origin.y>=-50 && rect.origin.y<=0){
            self.yourUIView.frame = rect;
    }
}

希望对你有帮助...

【讨论】:

    【解决方案3】:

    你需要为你的 tableView 实现 viewForHeader inSection 方法(记得让你的 ViewController 实现 tableviewDelegate 协议),一旦你得到它,你应该将你的 tableViewStyle 设置为 UITableViewStylePlain

    如果您对更多帮助感兴趣,请分享您的代码。

    来源:UITableView with fixed section headers

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题

      navigationItem.largeTitleDisplayMode = .always
      navigationBar.prefersLargeTitles = true
      

      和

      tableView.contentInset = UIEdgeInsets(top: 40, left: 0, bottom: 0, right: 0)

      对我来说,上述任何选项都无济于事,但我意识到你可以打电话

      tableView.setContentOffset(CGPoint(x: 0, y: -tableView.contentInset.top), animated: false)
      

      在viewDidload() 中,问题消失了! :)

      完全忘记了这种方法,并为这种令人毛骨悚然的行为花费了太多时间。

      祝你好运!

      【讨论】:

        猜你喜欢
        • 2013-01-20
        • 2010-10-18
        • 2014-08-11
        • 2011-12-17
        • 2019-03-17
        • 2019-06-15
        • 2013-10-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多