20

When I load my view controller I'm getting this error:

*** Terminating app due to uncaught exception 'CALayerInvalid', reason: 'layer <CALayer: 0x7fda42c66e30> is a part of cycle in its layer tree'

I don't know why. I think I might've added a third party framework that messed with layers, but I removed it in my troubleshooting. Any insights would be great.

EDIT

It happens during viewDidLoad of my tableViewcontroller. I have a tableView on my second vc. I've narrowed it down to it crashing on setting the heightForRowAtIndexPath on the 4th custom cell. The cell is on a static tableView................. :/ Getting closer!

Here's my heightForRowAtIndexPath:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    
    print("idx = \(indexPath.row)")
    
    return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}

Pretty generic. Any thoughts on where else this might be caused?

Community
  • 1
  • 1
Brie
  • 441
  • 1
  • 6
  • 14

9 Answers9

55

This might help someone still in trouble with this, For me the issue was this

layer.addSublayer(layer)

Adding a layer to itself ‍♂️ . After this mistake I am planning to kill myself :(.

infiniteLoop
  • 2,135
  • 1
  • 25
  • 29
31

check if you add self.view in self.view, exemple:

self.view.addSubview(view)

or

contentView.addSubview(self.view)
Pablo Ruan
  • 1,681
  • 1
  • 17
  • 12
21

I had the same error too and found this fix

It mentions to remove the accessoryView Outlet which you will see on the TableView.

To do this:

  1. Click on the Table View
  2. Go to the "Connections Inspector", it's the right window, the very last icon (looks like a circle with an arrow right)
  3. Look for the accessoryView and disconnect the outlet

In the screenshot below, see the outlet you are to disconnect:

enter image description here

Idee
  • 1,887
  • 21
  • 31
exo_duz
  • 326
  • 2
  • 5
  • This is exactly what was causing my issue. I got it resolved. Thanks –  Jun 22 '20 at 01:56
6

One of my friend was facing the same issue. The root cause was lying in the viewForHeaderInSection delegate of UITableview.

He was adding a label in main view of the controller in this delegate. He was supposed to create a UIView and return it in this delegate but unfortunately he was returning self.view there. So removing this code fixed the issue.

Amrit Sidhu
  • 1,870
  • 1
  • 18
  • 32
2

I've managed to fix it but not in the way that I'd hoped. I ended up scratching the tableViewCell that was causing the issue and rebuilding all the layout constraints from scratch. Not ideal, but it worked. I hope someone can come along in the future and explain why this error happened! Cheers.

Brie
  • 441
  • 1
  • 6
  • 14
1

I solved one crash of this kind by removing the outlet to File' s owner in some of xib UI files. I answer here in case someone encounters a similar thing.

xgdgsc
  • 1,367
  • 13
  • 38
0

I had this issue too, and found out I accidentally created an outlet from the TableViewCell backgroundView to the TableView's view - DOH!

jensk
  • 414
  • 5
  • 13
0

Also, when saving view as a variable inside UIViewController don't give it the name "view" cause "view" is saved to the UIViewController's View

Oz Shabat
  • 1,434
  • 17
  • 16
0

For me the problem was
I created a cell created its class and made some IBOutlet after some time I realised that my project already have class for this cell. So, I deleted my class and assigned existing class to my cell but the forgot to break IBOutlet of the old class So, solution is:
just go to Connection Inspector and remove all broken IBOutlet

Please have a look of the pic

Sultan Ali
  • 2,497
  • 28
  • 25