UITableView - a problem with the size and location of the background image

I want to set a background image in a UITableView when the list is empty. I installed it using the following code. It is clearly visible on the iPhone 5. But showing some space in the iPhone 6 devices.

Please, help. Im a beginner for iOS development.

@IBOutlet var tableView: UITableView!

let bgView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: tableView.frame.size.height))

let ivImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: tableView.frame.size.height))
ivImageView.image = image
ivImageView.tintColor = UIColor.green
ivImageView.contentMode = UIViewContentMode.scaleAspectFit
ivImageView.alpha = 0.05
bgView.addSubview(ivImageView)

tableView.backgroundView = bgView
tableView.backgroundView?.layoutIfNeeded()
tableView.backgroundView?.layoutSubviews()

Here is an example snapshot with an actual problem

+4
source share
2 answers

Your code is absolutely correct.

The problem is that you can set the background image during. viewDidLoad().Try using it instead viewWillApper(). It will set your frame with the exact size of your table view.

. Reset DispatchQueue.main.asyncAfter performSelectorWithDelay.

, .

0

, :   ivImageView.contentMode = UIViewContentMode.scaleAspectFit ivImageView.contentMode = UIViewContentMode.center

-1

Source: https://habr.com/ru/post/1670203/


All Articles