节标题卡在UITableView的中间

时间:2018-07-23 10:45:48

标签: ios swift uitableview uitableviewsectionheader

我目前正在尝试使用来自GitHub(https://github.com/gskbyte/GSKStretchyHeaderView)的'GSKStretchyHeaderView'库,通过'Parallex Header'实现TableViewController。向下滚动表格视图时,我还希望SectionHeader停留在视图顶部,该视图通常由iOS自动管理。

问题是-我想-由于Parralex标头被“强制”在表格视图的顶部,iOS感到困惑,并且节标题卡在表格视图的中间。 (https://youtu.be/AhsyNx2olRI

我尝试弄乱contentInsets,但是不幸的是,这没用。

这是我当前的代码:

class SightsDetailDynamicViewController: UITableViewController, GSKStretchyHeaderViewStretchDelegate, HeaderToParent {

var sight: Sight?
var stretchyHeader: PlaceDetailHeaderView?
var validReviews: [Review] = []

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let vc = segue.destination as? SightsDetailViewController{
        vc.sight = self.sight!
        vc.dynamicTableVC = self
    }
}

override func viewDidLoad() {
    setupHeader()
    setupTableView()
    loadValidReviews()
}

override func viewWillAppear(_ animated: Bool) {
    setupNavigationBar()
}

func setupTableView(){
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 100
}

func loadValidReviews(){
    for review in (sight?.reviews)!{
        if !review.text.isEmpty{
            validReviews.append(review)
        }
    }
}

func setupHeader(){
    let headerSize = CGSize(width: self.tableView.frame.size.width, height: 400)
    self.stretchyHeader = PlaceDetailHeaderView(frame: CGRect(x: 0, y: 0, width: headerSize.width, height: headerSize.height), place: sight!)
    self.stretchyHeader!.stretchDelegate = self
    self.stretchyHeader!.place = sight!

    self.stretchyHeader!.minimumContentHeight = (navigationController?.navigationBar.frame.height)! + UIApplication.shared.statusBarFrame.height

    if sight?.firstImage.size.height != 0{
        self.stretchyHeader!.maximumContentHeight = self.view.frame.height * 0.6
    }else{
        self.stretchyHeader!.maximumContentHeight = (self.stretchyHeader?.minimumContentHeight)!
    }
    self.tableView.addSubview(self.stretchyHeader!)

    if #available(iOS 11.0, *) {
        self.tableView.contentInsetAdjustmentBehavior = .never
    }
}

func setupNavigationBar(){
    navigationController?.setNavigationBarHidden(false, animated: true)
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.view.backgroundColor = .clear
    self.navigationController?.navigationBar.tintColor = UIColor.white
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
}

func stretchyHeaderView(_ headerView: GSKStretchyHeaderView, didChangeStretchFactor stretchFactor: CGFloat) {
    //maybe useful in the future
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! ReviewCell
    cell.authorNameLabel.text = validReviews[indexPath.row].authorName
    cell.ratingView.value = CGFloat(validReviews[indexPath.row].rating)
    cell.relativeTimeLabel.text = validReviews[indexPath.row].relativeTimeDescription
    cell.reviewTextLabel.text = validReviews[indexPath.row].text
    cell.profileImageView.image = UIImage()
    return cell
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Test"
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return validReviews.count
}
}

是否有可能告诉TableView节标题必须停止移动的地方,还是有其他解决方法?

很高兴得到您的帮助,谢谢!

0 个答案:

没有答案