Search code examples
swiftcagradientlayer

CAGradientLayer different result between iPhone SE and others


I have a CAGradientLayer, nothing complex

    let gradient: CAGradientLayer = CAGradientLayer()
    gradient.colors = [UIColor.clear.cgColor, Colors.Corporate.cgColor]
    gradient.locations = [0.0 , 1.0]
    gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
    gradient.endPoint = CGPoint(x: 0.0, y: 1.0)
    gradient.frame = bounds
    mainImage.layer.insertSublayer(gradient, at: 0)

I have a different result on iPhoneSE(Simulator) and iPod(Real Device)... any other device, iPhoneX(Real Device) and iPhone8(Simulator) work ad expected.

iPhoneSE Iphone8

The gradient overlay in the bottom part is different. Anyone else encountered this issue?


Solution

  • I think it depends on when the layer is created and inserted in the sublayer hieararchy. Can you try to update its frame on viewWillAppear (if you have access to the layer from view controller) or similar?

    A layer like that should always follow parent view's bounds when they change.