AnLT
AnLT

Reputation: 599

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?

Upvotes: 1

Views: 1032

Answers (3)

Ahmed Abdallah
Ahmed Abdallah

Reputation: 2355

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.

Upvotes: 1

johnyu
johnyu

Reputation: 2151

Instead of addSubview try using insertSubview(_ view: UIView, atIndex index: Int) with index 0, so it won't be above your cells.

Upvotes: 0

Ws_
Ws_

Reputation: 24

If you wanna reused the cell. You can try to add the ImageView into your tableViewCell. By the way, the header and the footer can be reused too.

Upvotes: 0

Related Questions