I'm having a problem with the "View as: *iPhone model*" inside interface builder in relation to launching the app in a simulator or physical device - my sublayer in the cell does not comply to the bounds set in the tableview. The thing is, if I run the app with the same model or smaller than the one chosen in "View as: *iPhone model*", all constraints, layers and sublayers function properly. But let's say the interface builder is set to "View as: iPhone SE", and I run the app in the simulator on iPhone 8, my sublayer do not match the bounds now.
Step by step to clear my problem:
First of all I have a regular UITableview where I insert different pictures. In order for these pictures to fit the screen as I want, I manually calculate the tableView.rowHeight inside a section:
let correctedHeightCellImage = cell.previewCellImage.frame.size.width / (pixelWidth) * (pixelHeight)
tableView.rowHeight = correctedHeightCellImage
This gives a correct height and in order to apply this change to the pictures I reload the data in the viewDidLoad:
DispatchQueue.main.async {
self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()
self.tableView.reloadData()
}
Now in my cell, I simply add a sublayer (gradientLayer):
import UIKit
class previewCell: UITableViewCell {
@IBOutlet weak var previewCellImage: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
DispatchQueue.main.async {
self.previewCellImage.addGradientBackground(colorOne: UIColor.clear, colorTwo: UIColor.black)
}
}
}
With the "addGradientBackground" being a UIView extension as so:
import UIKit
extension UIView {
func addGradientBackground(colorOne: UIColor, colorTwo: UIColor) {
clipsToBounds = true
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.colors = [colorOne.cgColor, colorTwo.cgColor]
gradientLayer.locations = [0.0, 1.0]
gradientLayer.startPoint = CGPoint(x: 1.0, y: 1.0)
gradientLayer.endPoint = CGPoint(x: 0.0, y: 0.0)
layer.insertSublayer(gradientLayer, at: 0)
}
}
Though the sublayer is cut out at the bottom if I have selected iPhone SE in the interface builder and the iPhone 8 in the simulator or a physical device. The same happens if I choose iPhone 8 in the interface builder and run the app on iPhone 8 Plus. You get the point.
Screenshot of the problem (the gradient is not covering the bottom part of the cell):
https://app.box.com/s/zxqs4gmqe1pdspv7a7gbxtlayk3ydid9
I'm really unsure how to fix this.
What are your thoughts? All help will be much appreciated!