0

I wanted to add a gradient color background to my application. Everything perfectly work except that my tableViews have disappear and my scrollViews snap their content in their background. How can I fix this problem?

func createGradientLayer() {
    let gradientLayer = CAGradientLayer()
    gradientLayer.frame = self.view.bounds
    gradientLayer.colors = [UIColor().HexToColor(hexString: "#783CBD", alpha: 1).cgColor, UIColor().HexToColor(hexString: "#BC1FFF", alpha: 1).cgColor]
    gradientLayer.startPoint = CGPoint(x: 0, y: 0)
    gradientLayer.endPoint = CGPoint(x: 1, y: 1)
    self.view.layer.insertSublayer(gradientLayer, at: 0)
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    createGradientLayer()
}
pacification
  • 5,838
  • 4
  • 29
  • 51
  • 1
    Set a breakpoint at createGradientLayer() in the method viewDidLayoutSubviews(). I think it is creating a lot of gradient layers... – J. Doe Sep 06 '18 at 10:38
  • The problem with the scrollView has been fixed. I did it by putting the function createGradientLayer() in the function viewDidLoad(). But the problem with my tableViews persists. – James DreeaMz Sep 06 '18 at 11:46

2 Answers2

0

This will insert a gradient layer every time the view lays out its subviews. I think UITableView doesn't like it if you mess with its internal layer structure.

I would just assign a gradient to the backgroundView property of the table view. See this for more info: Adding gradient layer to tableview

Rengers
  • 14,911
  • 1
  • 36
  • 54
-1

You maybe need to bring your table view and/or other views to the front. Try:

view.bringSubview(toFront: tableview)

Do it in viewWillAppear().

adrgrondin
  • 648
  • 7
  • 17