0

I have a screen with a UIImageView on top and a UITableView below it -

enter image description here

I want to move up and down the image view when the table view is scrolled so that the table view can take up the entire screen eventually and then come back into its original position when the table view contents are scrolled back all the way to top. I implemented this so far as below -

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    print("scrolled")
    print(scrollView.contentOffset)

    let currentOffset = scrollView.contentOffset.y

    let netOffset = previousOffset + currentOffset

    self.previousOffset = currentOffset

    if netOffset > 0 {
        self.imageViewTopConstraint.constant = -netOffset
    } else if netOffset < 0 {
        self.imageViewTopConstraint.constant = netOffset
    } else {
        self.imageViewTopConstraint.constant = self.imageViewOriginalTop
        self.previousOffset = 0.0
    }

}

The above code seems to be working fine but when the table view is scrolled all the way down the cells seem to flicker (May be because the scroll events continuously get passed due to bouncing effect when the table view contents are scrolled all the way up or down?)

How can I fix this?

cubsnlinux
  • 365
  • 3
  • 15

1 Answers1

0

You can add the UIImageView programatically as UITableViewHeader

@IBOutlet weak var tableView: UITableView!

let imageView = UIImageView(image: UIImage(named: "myImage.png"))
imageView.contentMode = .scaleAspectFit
tableView.tableHeaderView = imageView
Abel C
  • 62
  • 4