Add custom view on top of other UITableViewCell's - ios

I have a tableView with cell and in a specific cell i want to add a view that is bigger that the cell and is is overlapping the other cell where i want to add buttons, labels and so on.
The problem is on scrolling and on touch. I tried bringSubviewToFront: and also set the zPosition to 3 and also i setClipsToBounds: to NO but as you can see in the hierarchy my custom views are under the cells.
My question is how to bring my custom view that are child of a custom cell in front of all cells and how to make all area to respond to touch events.
this is what i want to achieve, This happens only at the first start and gesture recognisers respond only over the parent cell.

You have to disable bounds clipping on your cell's view:
[cell setClipsToBounds:NO];
If the view that must overlap multiple cells is embedded in a subview, make sure that this subview has its clipsToBounds property set to NO too.

Related

Button not responding in Table View Cell [duplicate]

This question already has answers here:
Programmatically send to front/back elements created from interface builder
(2 answers)
Closed 10 days ago.
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:
Structure of .xib
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.
Maybe you forgot assign your Button to code
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.
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.
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.
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.

Disabling touch interaction of Spaces between Custom table view cell

I use custom cells of subclassing UITableViewCell in a table view. And There must be space between cells. I succesfully add the space with adding a subview that I called SpaceView into the content view. But When I touched the spaceView, it is perceived as I touched the cell. And didSelectRow method called. I tried to set spaceView's UserInteractionEnabled==NO. But It doesn't work.
So my question is; Is this the right way I used to add space between cells ? Should I try to add cells for making the space ? or If its the right way, How can I prevent calling "didSelectRowAtIndexPath" method when I touched spaceView ?
like Inder said below, u can use an UIButton to solve the issue.
1) in your custom cell: at the bottom of your custom cell add a blank UIButton with the height u actually need between cells, and customize its background color according to your needs.
2) in cellforrowatindexpath: disable button of each cell. (or you can also do that in Interface Builder of previous step)
result: u have a clear disabled button that will appear as required space between cells.
Make your spaceView as UIButton (I guess it's UIView right now)
When you add UIButton that touch will be consumed by the button thus touch won't be passed to it's parent.
OR
I'm not sure if it will work or not, you can add tap gesture to your spaceView

UITableView with a UIView underneath the Cells

I have a UITableView and below the custom cell (in IB) I have inserted a UIView. The UIView gets set to hidden when viewDidLoad() gets called and is only displayed when there is no data in the UITableView. This works great and servers my purpose.
The problem is that even when the UIView is hidden, the scrolling on the UITableView considers the view to be present. i.e. The vertical scroll with scroll well below the last cell, covering the area where the UIView exists - even though its blank and nothing is displayed.
I have tried to hide the UIView but this doesn't help either. How do I get the scroll to not extend to the area covered by the UIView?
Edit:
The image on the left is how IB looks. I have added a UIView as a subview of the TableView. The image on the right is the large scroll space below the last cell.
Expected behavior: When the view is hidden, the scroll view only scrolls until the last cell.
If your view is displayed only when there is no cells, you could just add it over your UITableView (or behind it if your UITableView's background is clear) and hide it when you have cells to display. Just be sure to add your view as a sibling of your UITableView and not as a child view of your UITableView.

uiview on top of uitablecell

I have a custom table view with a custom table view cell.
I have a uiview with three buttons. How can I set that when a row is selected the uiview will popup over the cell? Basically I want a popover controller. Should the uiview be inside by table view controller?
I'm targeting iphone and cannot use people's project.
It depends on whether your popover view fits entirely within the cell.
If so, when you build the cell, add the popover view and mark it hidden. Then, when the delegate is told that the cell has been selected, change hidden to NO (you could even play with the alpha and wrap that inside [UIView animateWithDuration:animations:] to have it fade in).
If not, then you'll want to have the popover view ready ahead of time, and add it to the window directly when the cell is selected. You'll want to use the frame of the cell and the [cell convertRect:to:] method to position your view appropriately. You will also want to make sure that you add a "background" UIView to ensure that stray touches can't reach the UITableView underneath.

TableView doesn't not scrolling and cells is not enabled

I've created UIImageView with tableView and put this view on another view. But my table isn't enabled. I can't press on the cell or scroll my table.
Try creating a UIView with your UIImageView and UITableView as subviews.
I don't think it's a good idea to have your table as a subview of an imageview.
I suppose you did this to have a background image for the table, but a transparent table with an image view behind can easily achieve this effect.
Do you have a UITableView as a subview of a UIImageView?
If that is the case then the problem is probably that userInteraction is disabled on your UIImageView (it is set to NO as default for UIImageViews).
You can try to set it to YES and see if you can interact with your UITableView. However I would not recommend this view structure as I don't believe it is intended to put UITableViews as subviews to UIImageViews

Resources