Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS9 - UITableViewCellContentView is covering up Controls inside Cell

I have made a custom UITableViewCell called "SwitchCell" that has a switch. In iOS9 Only, using Xcode 7 beta, the Content view in the cell is on top of the switch. (See screenshot of View Hierarchy. You can clearly see that the content view of the cell is on top of the other views. ): enter image description here

So all the touches to the UISwitch are intercepted, and the IBAction does not fire. In iOS8, this is not a problem. See screenshot for iOS 8.4 simulator. You can see that there is no content view on top of the controls: enter image description here

Has anyone had this problem? I tried remaking the NIB from scratch, but the same result occurs.

My NIB is a freeform size view with No status bar. It has two outlets: one for UILabel, one for UISwitch.

EDIT: please make sure to check the answer below that asks to verify that the cell's root view is not just a UIView but a UITableViewCell. This issue may also be a side effect of this.

like image 836
FranticRock Avatar asked Jul 28 '15 15:07

FranticRock


4 Answers

After more investigation and searching, i found my solution here: Button in UITableViewCell not responding under ios 7

What fixed it for me was:

cell.contentView.userInteractionEnabled = NO;

This prevents the cell content view from taking over the touch events, even though it's on top of the other views.

This issue was not only happening on iOS9, but on iOS7 as well. In iOS8, the Content view was behind the controls.

like image 100
FranticRock Avatar answered Nov 11 '22 22:11

FranticRock


Problem can be in .xib file for your cell. When you create cell in separate .xib, be sure to drag UITableViewCell on canvas, not UIView.

like image 35
Alexandra Avatar answered Nov 11 '22 22:11

Alexandra


SWIFT

I had an issue where my buttons in my custom tableviewcell swift files were working just fine, but then I upgraded to Xcode 12 and then all of a sudden I couldn't access them anymore (meaning my taps were not being recognized). The cell content view seemed to be interfering in the hierarchy and this like saved me:

cell.contentView.isUserInteractionEnabled = false

I put the line in cellForRowAt.

Thank you to @FranticRock

like image 4
A.J. Hernandez Avatar answered Nov 11 '22 23:11

A.J. Hernandez


I had this problem when I was reusing a cell as a .xib. I didn't realise but when I first created the xib the default view that it created was in fact a UIView and not a UITableViewCell. It seems that at some stage UIKit adds the content view on top of my other elements and therefore interrupts certain events (e.g. touch events).

I resolved this by opening my xib file and dragging a UITableViewCell onto the canvas and copying my UI elements from the old view to the new cell.

Afterwards, additional settings also became available in the attributes inspector that matched those for a UITableViewCell.

like image 2
Gordonium Avatar answered Nov 12 '22 00:11

Gordonium