masksToBounds property of UITableViewCell's content view not working - ios

I have a UITableView with a prototype cell in it. The cell has a content view property which (I assume) refers to the content view which is in the cell by default. I've added a UIImageView into this content view (for a background image) and I've set the clipsToBounds property of this image view to true to make sure it doesn't show past the borders of its parent view. In addition to this, I've also set the masksToBounds property of the parent view (the content view of the table view cell) to true, which to my knowledge should be hiding its subviews outside the borders. However, if I set the cornerRadius of the content view to 45, the background image view inside the content view is still visible outside the borders. I've triple-checked to make sure all the properties are pointing to the right views and everything seems to be okay, but for some reason the content view of the table view cell doesn't want to mask to its bounds. Does anyone have any experience with this? If so, how do I deal with this?

contentView of the UITableViewCell is also changed internally before being displayed. I recommend you to make clipsToBounds in willDisplayCell method of the UITableViewDelegate, or put the same sized (constrained, autoresized) UIView on top of the contentView, setting contentView.backgroudColor = UIColor.clear, and moving all contentView's subviews to the view. Hope this helps, good luck!

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.

iOS: Stretching ImageView Above My TableView?

I have added a UIImageView on top of my tableView in storyboard & it works perfectly fine, except that when you scroll down, the imageView doesn't stick to the navigationBar and instead only sticks on to the tableView (revealing the view's background above it).
How would I get it to stick to both the tableView and the navigationBar, and just have the imageView stretch/zoom as the user pulls the tableView down?
This is how it's set up in storyboard:
And this is how I assign an image to it in my ViewDidLoad:
childHeaderView.image = [UIImage imageNamed:headerImage];
Any help would be greatly appreciated. I've tried setting constraints on it using autoLayout but it doesn't seem to let me (they're grayed out, even though I've enabled it for that ViewController).
The problem is that what you did in storyboard is adding the UIImageView as a header to your UITableView.
The proper way to do this is to add the UIImageView at the same level as the UITableView, which mean embed these two views inside a UIView.
Having a UIView as a root view for a view controller is unfortunately impossible for a UITableViewController, and I fear that this is your case. So you may want to replace your UITableViewController subclass by a UIViewController subclass.
EDIT: You'll want to set a fixed height constraint on your UIImageView, and add a vertical space constraint with a 0pt value between your UIImageView and UITableView.
Most of these can be achieved by moving view in IB.

Getting real width of UITableViewCell's contentView before it displays

I have a grouped table view with custom cells (created programmatically by subclassing, not with IB). To properly position custom cell's subviews (such as labels and text fields), I need to know the current width of the cell's contentView just before the cell displays (taking into account that real cell width in a table view can change (according to screen orientation, modal presentation style, etc.)).
if I override in custom cell class the layoutSubviews method, it works perfectly, but it can be called frequently, thus I have to reposition my subviews every time when it's called, even when there's no need to do that.
Please, recommend me more elegant solution.
The recommended way of doing this is by setting the autoresizingMask of the table cell. If you need to have more control over the layout, you can store the last used view width in a member variable, and only layout the subviews if this differs from the current view width.

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

Why does UITableViewCell have a contentView property?

What's the point of this contentView property? I mean: Why aren't all the subviews just added to self?
Let me get that right: Every cell is a view (UITabvleViewCell is a UIView subclass). And this fat view has another fat view with same bounds sitting on top of it, called contentView. That contentView then carries all those other subviews.
Now why didn't they save that extra chunk of memory? Is there any genius logic behind this decision? Would love to understand the reason for this.
The content view does not always have the same bounds as the cell. For instance, when entering editing mode, the delete button slides the content view to the right, resizing it in the process.
From A Closer Look at Table View Cells
Figure 5-1 Parts of a table view cell:
Figure 5-2 Parts of a table-view cell in editing mode:

Resources