1

I am using a .xib for my cell in a table view. Inside the .xib, is another view (cellBackgroundView), and a button. When I run the app, and click the button, it does not respond at all. Instead, it calls the tableView's didSelectedRowAtIndexPath method which brings another view controller.

Using Xcode's Debug View Hierarchy, I discovered that I have a view overlaying the all the buttons (see pic attached: this overlaying view is highlighted). This view (called backgroundView) that is overlaying my button is a View, within a view. I have a feeling when you place a view in a view, and put a button in the initial view, the button isn't called because its below the view hierarchy.

How do I fix this issue? Is there a way to move background view to the back of the view heirarchy so that the buttons will be responsive?

Debug View hierarchy:
enter image description here

Structure of .xib
enter image description here

Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98

6 Answers6

1

Two things that you could check

1) Do you have a delegate method for cell height and is the height returned correct? Unless you have Clip Subviews on for the UITableViewCell, the contents of the cell can be visible outside it's frame, but the parts that are outside the cell's frame are not registering user interactions.

2) Is some other view element higher in the hierarchy (lower in the XCode listing you posted) overlapping the button? iOS Simulators Debug -> Color Blended Layers can help spotting this.

Edit: If I interpret the added screenshots correctly, you probably have the issue mentioned in the option 1) above. If the other elements showing in the screenshot are those listed as subviews of the Cell Background View they are mostly outside the parent view's frame and thus don't receive touch events. If the background view's frame is correct, then you might want to move the other elements as children for Feed Cell directly.

Also, the element listing suggests that you are using plain UIView as the parent element. I don't know the inner workings of your application, but if you only use this view in a UITableViewCell you might want to consider making the parent view a Table View Cell in the xib. This will reduce some bloat and allow you to configure some properties for the cell in the xib.

lekksi
  • 4,868
  • 1
  • 16
  • 13
  • I actually have a view element higher in the hiearchy. How do I push this view backwards? This view is called backgroundView (see updated question). – Josh O'Connor Oct 12 '15 at 01:48
  • @JoshO'Connor you can drag and drop the elements in the listing on your second screenshot. Note that the elements lower in the list are the ones having higher view index (`Profile Image View` has lower index than `Username Label`), and thus will be on top of any previous element. See my updated answer for more details. – lekksi Oct 31 '15 at 23:40
0

Maybe you forgot assign your Button to code

Tà Truhoada
  • 584
  • 7
  • 23
  • No there is working code. Ive created a simple view and it responds. It isnt responding because its in a view, within a view (had to do that for autolayout) – Josh O'Connor Oct 12 '15 at 02:00
0

I assume Feed Cell is a subclass of UITableViewCell, and cellBackgroundView is the property contentView of this cell.
If so, the cells property backgroundView should be behind your cellBackgroundView (the docs say: UITableViewCell adds the background view as a subview behind all other views and uses its current frame location.).
You could set the cells property backgroundView = nil, and see whether it is still there in the view hierarchy. If so, you do add a custom backgroundView on top of the other cells views somewhere.
To check this, you could read out the subview hierarchy of your cell in your method tableView:cellForRowAtIndexPath: with something like NSArray *svs = cell.subviews; (assuming cell is the tableViewCell) and set a breakpoint behind this statement.
svs usually contains first the UITableViewCellContentView, and above it the _UITableViewCellSeparatorView. The cells backGroundView will not be shown. You could check there your view hierarchy.
If by chance there is a custom backgroundView on top, you could - as a workaround, not a solution - bring the contentView to the front by sending to the cell bringSubviewToFront: with the contentView as argument. Then the button should respond.

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
0

In your Structure of Xib Place your button below the view that is first the view is added to superView then the Button, then your button will work.

or you can code

-(void)viewDidLayoutSubviews{

[self.view insertSubview:yourButton aboveSubview:cellBackgroundView];

}

Hope it will help.

Faran Ghani
  • 277
  • 4
  • 17
0

Do you add an UIButton by code?
If so you should ensure you addSubview: into cell.contentView and not into cell.

Also you could try to apply CellBackground class to a view inside contentView, not directly to contentView.

blld
  • 1,030
  • 10
  • 19
0

you can use this method.

[cell.contentView  bringSubviewToFront:yourButton];

after that if you want to back in background then tou can use sendSubviewToBack: method .

After adding this methods your button is not responding set the userIntractionEable of button's superview.

Garry
  • 407
  • 2
  • 15