Search code examples
iosswiftuitableviewuiscrollviewparallax

Swift - UIScrollView Extension- Add Image to A UITableView


When I use the StoryBoard, I can add an UIImageView to UITableView like this:

enter image description here

I am trying to write an UIScrollView extension to add a parallax image into a UITableView which can be reused for all the tables easily. Here my code to add image:

 extension UIScrollView {

  func addParallaxImage(image image: UIImage, height: CGFloat) {
    let myImageView: UIImageView = UIImageView()
    myImageView.image = image
    myImageView.contentMode = UIViewContentMode.ScaleAspectFill

    myImageView.frame.size.width = UIScreen.mainScreen().bounds.size.width
    myImageView.frame.size.height = height
    myImageView.frame.origin.y = 0

    self.addSubview(myImageView)

  }

}

Function usage:

tableView.addParallaxImage(image: UIImage(named: "pets")!, height: 100)

However, my image overlaps the table.

enter image description here

Any solutions?


Solution

  • The best solution for your case is used tableview header and here the most usable libs:-

    CSStickyHeaderFlowLayout

    ParallaxTableViewHeader

    VGParallaxHeader

    APParallaxHeader

    Take care not all libs support Swift, I hope I help you.