What are pros / cons to add UIView to cell vs cell.contentView? - ios

I have a UITableViewCell. How should I add a subview to it? I have seen many example to add subview to the contentView, but why not directly to the cell?

You can do it, but If you want to customize cells by adding custom view, you should add them to the contentView in order to get them animated along the table animations

When you add a custom view to contentView or assigning to contentView handles framing resizing part ,animation part and when editing mode is ON (tableView.editing = YES;) tableview tries to operate with views of itself meaning contentView, accessoryView. So when you want to add something to the table view and let control be given to tableView go for contentView

Related

How to add a scrollable view within a collectionView cell

I need to add a scrollview within a collectionViewCell. This is confusing me because there are essentially two ways of scrolling. You can either scroll from one collectionView cell to the next or within a collectionView Cell itself, to get more info from inside the cell. I have attached a picture of what my collectionView cell looks like right now. I would like for the scrollview to scroll within the gray view. How should I set this up in my interface builder?
Thanks so much!
I have also attached the current heirarchy for my scrollview *Scrolling functionality is not working at the moment.
You have a custom collection view cell already just add a scrollView and then anything you want to display in the cell. So your hierarchy would be something like this :
Custom-Cell: ContentView > UIScrollView > then anything you want to add in the view.

Issue with Autolayout and StackView while using ScrollView

I a trying to create a UIScrollView that fits exactly the contents of its subviews. I do this with a StackView and the scroll view's height is determined by how much content there is in the stack view. Within the stack views, there are views that contain an UIImageView and a UITextView
The UIScrollView starts below the title view, however every time I want to add another view (which it should scroll when the content is bigger than the actual frame) there is an issue with the scroll view's Y position. This works perfectly if I only add UILabels and UITextFields, using the same procedure
How can I do it so I can programmatically add views on the stack view that includes an UIImageView and a UITextView, just as the View Controller with labels and textfields does.
You can download my app project at the following url
https://github.com/francisc112/DescriptionWithImageApp.git
Instead of a scroll view why don't you use a table view and create a custom cell with the stackview and its contents inside the cell and you can dequeue the same cell for adding multiple information.
I was not able to open your project
So I Tried opening your StoryBoard and Re-Viewing your constraints Applied , here is what I have Done Please check in following Link
Link - https://drive.google.com/file/d/1D-dzKsSqpx7dWsI5fYRdAfCMY_vENoIR/view?usp=sharing
View Hierarchy
Note - For such output I will prefer using a TableView instead of StackView as Same output can easily be achieved using TableView

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.

Removing a static UIView from a UITableView with Prototype Cells

I have a storyboard I'm maintaining that is a UITableViewController.
The UITableView is set to use Dynamic Prototypes and has 2 cells.
Above those two cells is a UIView inserted within the table view. That view contains a UIButton and another custom UIView. Both have constraints. The effect is that we have a table view with a button above it.
Now, in certain conditions, we want to remove that button and have the rest of the table view move up in the UI. I can remove that button -- by itself or by removing that parent UIView, but I cannot seem to to get the table to change the position of the prototype cells.
To hopefully make it clear, my document outline looks like this:
Table View
View (buttonContainerView mentioned below)
UIButton
View (child view mentioned below)
Constraints
Prototype Cell 1
Prototype Cell 2
I have tried several different approaches to remove the button. After reading that removing it from the superview will remove constraints as well, that's the approach I currently have in place.
[self.buttonContainerView removeFromSuperview];
I've tried removing the button, the child view, and the container view. I've also tried explicitly removing the constraints then removing the views. None of these seemed to work.
I've also tried the above with calls to setNeedsLayout and setNeedsUpdateConstraints on the table view, as well as setNeedsDisplay on the view.
I've also tried setting the row height to 0 for that first view, but it turns out it's not a UITableViewCell, so that doesn't work, either.
Of course, if I delete that container view from the storyboard, the prototype cells do appear in the UI exactly where they should.
Can you sense the flailing about?
What else could I be missing here to remove that top view effectively?
What I didn't realize is that the setup I have is a UITableView with a table header. To remove it and fix the spacing problem I was having I just needed to set self.tableView.tableHeaderView = nil in viewWillAppear:.

How to add a subview to a UIView's contentView in a UIStoryboard? (iOS)

I have a UITableView that I've created in a UIStoryboard. It contains one Prototype UITableViewCell. The cells are populated in the ViewController's cellForRowAtIndexPath: method.
I'm now trying to add the ability to delete cells using commitEditingStyle:forRowAtIndexPath:. Everything is working except that when the delete button animates over the cell, the rest of the content doesn't shift over and it looks really ugly. According to this SO question, content will only be shifted if it's added to the cell's contentView rather than to the cell itself.
As such, is there a way to add a UIView (Ex. a UIButton) to a UITableViewCell's contentView within a UIStoryboard?

Resources