简体   繁体   English

段 Controller 不能正确滚动

[英]Segment Controller not scrolling properly

I am trying to implement a method to where I have a tableviewcontroll that has a header where it shows a head picture as well as another section with the User's information and Profile picture.我正在尝试实现一种方法,我有一个 tableviewcontroll,它有一个 header,它显示一个头像以及另一个包含用户信息和个人资料图片的部分。 Below the head I want there to be a segment control like twitter that will allow a user to switch between different tableviews.在头部下方,我希望有一个像 twitter 这样的段控件,它允许用户在不同的表格视图之间切换。 I want that segment control to be below the header view so when a user pulls down, the segment control moves down with the header view, when you scroll up the segment control moves up with the headerview but then sticks below the navigation bar similar to how twitter and instagram's UI works.我希望该段控件位于 header 视图下方,因此当用户下拉时,段控件会随着 header 视图向下移动,当您向上滚动时,段控件会随着标题视图向上移动,但随后会粘在导航栏下方,类似于twitter 和 instagram 的 UI 工作正常。 I have implemented the code below but I am getting an issue to when I scroll up, the segment controller doesn't move but when I pull down on the screen the segment control moves down with the header view.我已经实现了下面的代码,但是当我向上滚动时,我遇到了一个问题,段 controller 没有移动,但是当我在屏幕上下拉时,段控件随着 header 视图向下移动。 Any Idea why it is doing this?知道为什么要这样做吗?

Here is the code这是代码

class RandomTableViewController: UITableViewController {

    @IBOutlet weak var trackImage: UIImageView!

 private let tableHeaderViewHeight: CGFloat = 320.0
        private let tableHeaderViewCutAway: CGFloat = 0.1

        var headerView: HeaderView!
        var headerMaskLayer: CAShapeLayer!

        override func viewDidLoad() {
            super.viewDidLoad()

            self.automaticallyAdjustsScrollViewInsets = false

            tableView.estimatedSectionHeaderHeight = 40.0

            navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
            navigationController?.navigationBar.shadowImage = UIImage()



            tableView.tableFooterView = UIView()

            headerView = tableView.tableHeaderView as! HeaderView
//            headerView.imageView = trackImage
            tableView.tableHeaderView = nil
            tableView.addSubview(headerView)

            tableView.contentInset = UIEdgeInsets(top: tableHeaderViewHeight, left: 0, bottom: 0, right: 0)
            tableView.contentOffset = CGPoint(x: 0, y: -tableHeaderViewHeight + 64)

            //cut away header view
            headerMaskLayer = CAShapeLayer()
            headerMaskLayer.fillColor = UIColor.black.cgColor
            headerView.layer.mask = headerMaskLayer

            let effectiveHeight = tableHeaderViewHeight - tableHeaderViewCutAway/2
            tableView.contentInset = UIEdgeInsets(top: effectiveHeight, left: 0, bottom: 0, right: 0)
            tableView.contentOffset = CGPoint(x: 0, y: -effectiveHeight)

            updateHeaderView()
        }

        func updateHeaderView() {
            let effectiveHeight = tableHeaderViewHeight - tableHeaderViewCutAway/2
            var headerRect = CGRect(x: 0, y: -effectiveHeight, width: tableView.bounds.width, height: tableHeaderViewHeight)

            if tableView.contentOffset.y < -effectiveHeight {
                headerRect.origin.y = tableView.contentOffset.y
                headerRect.size.height = -tableView.contentOffset.y + tableHeaderViewCutAway/2
            }

            headerView.frame = headerRect

            let path = UIBezierPath()
            path.move(to: CGPoint(x: 0, y:0))
            path.addLine(to: CGPoint(x: headerRect.width, y: 0))
            path.addLine(to: CGPoint(x: headerRect.width, y: headerRect.height))
            path.addLine(to: CGPoint(x: 0, y: headerRect.height - tableHeaderViewCutAway))

            headerMaskLayer?.path = path.cgPath
        }

          override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
              self.tableView.decelerationRate = UIScrollView.DecelerationRate.fast
          }

          override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
              return UITableView.automaticDimension
          }



        @IBAction func backButton(_ sender: Any) {
            dismiss(animated: true, completion: nil)
        }

    }

    extension RandomTableViewController {



        override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
             let v = UIView()
            v.backgroundColor = .white
             let segmentedControl = UISegmentedControl(frame: CGRect(x: 10, y: 5, width: tableView.frame.width - 20, height: 30))
             segmentedControl.insertSegment(withTitle: "One", at: 0, animated: false)
             segmentedControl.insertSegment(withTitle: "Two", at: 1, animated: false)
             segmentedControl.insertSegment(withTitle: "Three", at: 2, animated: false)
             v.addSubview(segmentedControl)
             return v
        }



        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            100

        }

        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "CellID")
            cell.textLabel?.text = "\(indexPath.row)"
            return cell
        }


    }


    extension RandomTableViewController {
        override func scrollViewDidScroll(_ scrollView: UIScrollView) {
            updateHeaderView()
        }
    }

I have custom HeaderView code which is我有自定义 HeaderView 代码是

import UIKit

class HeaderView: UIView {


    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var trackImage: UIImageView!
    @IBOutlet weak var backButton: UIButton!

}

Here is what it is doing这是它正在做的事情

图像1

图2

UPDATE:更新:

I implemented the grouped tableview and setting the estimatedSectioHeaderHeight in the viewForSectionInHeader and now the segmented control moves up and down but it is not sticking below the navigation bar when scrolling up.我实现了分组表视图并在 viewForSectionInHeader 中设置了estimateSectioHeaderHeight,现在分段控件上下移动,但向上滚动时它没有粘在导航栏下方。 Also there seems to be a formatting issue now.现在似乎还有格式问题。 Here is how the simulator looks这是模拟器的外观

在此处输入图像描述 在此处输入图像描述

change the table style to grouped and set height of the segment view in heightForHeaderInSection.将表格样式更改为分组并在 heightForHeaderInSection 中设置分段视图的高度。 Check whether it works.检查它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM