Marc Becker
Marc Becker

Reputation: 553

Make background image fixed

I am working on a swift app right now, that uses a table view to display news. For the background I set an image using the following line of code in the viewDidLoad method.

self.view.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundImage.jpg")!)

however this makes the background repeat itself when scrolling down, which makes it look very ugly (see attached image).

Background image keeps repeating when scrolling

So what I desire is to have a fixed (non-repetitive) background image that always looks the same while scrolling and does not move at all.

How can I achieve that?

Thank you for your help!

Upvotes: 0

Views: 996

Answers (2)

Marc Becker
Marc Becker

Reputation: 553

So I did some more digging and finally came across this post: https://stackoverflow.com/a/27684597/2204207

There it suggested to use

self.tableView.backgroundView = UIImageView(image: UIImage(named: "backgroundImage.png"))

instead of

self.view.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundImage.png")!)

Therefore, when you want a fixed and non-repetitive background image use .tableView.backgroundView. This fixes the background images even when scrolling down.

Upvotes: 3

Emma
Emma

Reputation: 9363

You can make a view hierarchy like this.

enter image description here

Make ImageView and TableView as same size and place table view below the ImageView in view navigator.

Then add your background image to the ImageView and make TableView's background to clear color.

Upvotes: 0

Related Questions