Problem description

A few days ago, I found such a bug in the adaptation of Xcode12 + iOS14, because when adding a view on the cell, I did not add the Button to the contentView of the TableViewCell, but directly to the TableViewCell. The Button’s click events are all unresponsive.

The contentView is now at the top of the view.

To explore the process

The first intuition is that in XCode12 + iOS14 apple changed the underlying implementation and put the ContentView on the top layer, which caused this problem (this is not the case, we’ll explain why later).

I am not so busy these days and I plan to explore the reasons for this problem in detail:

Get the exact time and call stack for the TableViewCell to add contentView by adding -[UIView addSubview:] Symbolic BreakPoint.

When ContentView is added to Cell:

Before iOS14:

ContentView is in[UITableViewCell initWithStyle:resueIdentifier]Is the first to be added to the View when initialized. So at the bottom.

On the iOS14:

ContentView is generated using lazy loading. When the cell is about to display, call-[UITableView _configureCellForDisplay:forIndexPath:]_block_invokeYou need to adjust the layout of the ContentView and load the ContentView onto the Cell. So there’s the bug that the ContentView is on top.

Then by comparing the iOS13 and iOS14 initialization method of Cell [UITableViewCell initWithStyle: reuseIdentifier:] breakpoint debugging.

It turns out that there’s a lot of Cell init methods on iOS14_UITableViewCellEnableLazyContentViewIn iOS14, ContentView is generated using lazy loading._UITableViewCellEnableLazyContentViewIs a method that returns true by default, if trueThe ContentView is not initialized by default in Cell initialization methods. Instead, the ContentView is created and added to the Cell at call time.

The Test:

If you’re really lazy, you don’t need to add the custom view to the contentView, just call the contentView before the custom view is added to the view, and the bug goes away.

Here’s a test:

Write the simplest cell here, adding only a red button. Only in theaddSubView(redButton)I’m just going to call the lazy method of the contentView, and I’m going to give it a green background color.

At this point the green contentView is back behind the redButton 😅😅. It’s normal.

If you’re interested, you can write a Demo.

Comprehension:

This is an Apple bug.

May be the epidemic when which a SAN at home state is not well considered completely write such logic.

This bug has not been fixed in Xcode12.2 and iOS14.

Probably not going to fix it.

As an Apple developer, my happiness has been plummeting lately.