0

I'm currently trying to implement a TableViewController with a 'Parallex Header' using the 'GSKStretchyHeaderView' library from GitHub (https://github.com/gskbyte/GSKStretchyHeaderView). When scrolling down the tableview, I also want the SectionHeader to stay on top of the view, which is usually automatically managed by iOS.

The problem is, that - I suppose - because of the Parralex header that is 'forced' on top of the tableview, iOS gets confused and the section header gets stuck in the middle of the tableview. (https://youtu.be/AhsyNx2olRI)

I tried messing around with the contentInsets but that didn't work unfortunately.

That's my current code:

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
}
}

Is there any possibility to tell the TableView where the Section Header has to stop moving along or is there maybe some other workaround?

I'm happy for any help, thanks in advance!

Medwe
  • 318
  • 1
  • 12
  • instead of adding header as a subview try to add it as header. "self.tableView.tableHeaderView = self.stretchyHeader! " – ManuRaphy Jul 23 '18 at 10:50
  • I tried and it seems the library continuously puts the header in some kind of contentView on top of the tableHeaderView. When assigning the stretchyHeader to the tableHeaderView it jumps up and down as soon as I scroll. – Medwe Jul 23 '18 at 13:11

0 Answers0