How do I add spacing in the UITABLEVIEW between cells? - uitableview

Hi i am new to swift and i am looking for a way to make space between cells.
I have tried different types of code found here but neither works.
How do you make spacing between Cells in the uitableview?

If you are using auto layout, you can embed your cell design inside a view and add top and bottom spacing to that view with respect to content view.
Consider below screenshot, the red coloured view contains all the design and have top and bottom spacing with content view.

Related

iOS Autolayout: How to show / hide a view including its margins?

Assume the following, simple layout:
Three views vertically stacked upon each other
Using simple vertical spacings between the views
Is it possible to hide the red view including its margins using constraints / AutoLayout only?
Settings redView.isHidden = true will hide the red view but will not change the position of the blue view. The blue view will stay at the same position as if the red view would be visible.
Using redView.removeFromSuperview() to completely remove the red view would show the desired result. Due to its optional spacing constraint to the gree view the blue view would move to where red view was. However it would be quite hard to re-show the red view because all its constraints would have to be set up from scratch.
In Android setting the visibility to View.INVISIBLE simply hides a view (as the first case described here) while View.GONE renders the remaining layout as if the view was not there at all.
Can this be done with iOS using constraints / AutoLayout only?
Of course I can achieve the same buy manually manipulating the constraints and setting up new constraints in code. But the question is, if there is a more convenient solution as in Android?
A VerticalStackView seems to fit your requirements. You can include all the views in the stack view and set the spacing directly on it.
Then, is one of the views is hidden, the stack view will automatically adjust all the constraints.
Take a look at the pictures:
If possible, wrap your views in a vertical UIStackView. You can then individual views and the other views will be rearranged as intended. You also don't need to add constraints between items, since the stackview handles the spacing between views.
The simplest way is to embed the views into a StackView and when one of them is hidden, the one below will move up into its place.
Follow these steps:
Add all the views you need in the storyboard/xib
Editor - Embed in stackView
Set the spacing in the stackView
Set the stackView constraints
Create outlets for the views you want to be hidden in a certain case
Set that views hidden property to true
Regarding the constraints, you can set them for the StackView and for the vertical one, just set the equal spacing and the space properties in the StackView.
Result:

Auto Layout: Set height View depends on other height View

I need to implement AutoLayout in my layout. Below are some screenshots:
1.
2.
The case is :
I have a ShinobiDataGrid in View and a UITableView . First image is first condition, in first picture I want the view have height as height View Controller.
The second and third picture is when there are available TableView . I want the TableView always is at the bottom of View Controller and the View is have height depends of height Table View.
I read this, this and all of reference on that but it's not working for me.
Edit
I read some article about Auto Layout and I get this below:
1.
2.
But in first picture, the the UIView still have a distance with main View. I want it only have 3 or 4 point from bottom of main View like below:
Now, I have one constrain from UIView to the UITableView. I add one constrain from UIView to bottom main View with custom priority, constant and etc but it's not working.
1.I would suggest you to use UITableView with section header and footers , with no scroll. Add your views to UITableViewCell. Populate your UITableView with custom cells based on your requirements.
2.You can also use UIStackView for the same. Add all your views & table views to a vertical stack view in Interface Builder. Keep distribution as fill equally. First time hide your UITableView. Your view will cover complete then show / hide tableview when required and update stack view.
Constraint your views & table views with respect to stack view. UIStackView manages autolayout.
3.I would have used UITableViewController with static cells (with no scroll) with views and tableviews as subview of static UITableViewCells. First time in heightFoRowAtIndexPath pass complete height. When tableview will be visible. Reload tableview controller and pass height accordingly.
Instead of using UITableView inside cell. I would suggest using UIStackView as subview cell. Show your data in UIStackView.
First add scrollview.
Add the height constraint of First View equal to view controller.
Below to that, add the tableview, get the dynamic height and change according to it. You can see my answer here for this.

ios - Vertical position is ambiguous

I've a regular table view and I'm putting a view above and below the cell currently in the middle. On some occasion, those "above" and "below" cells don't appear because Vertical position is ambiguous. I don't see the ambiguity. My views structure looks like
ans here's a screenshot of the contraints on the above cell:
As you can see, constraints related to vertical positioning are:
bottom of the view
height
And I don't see how this leaves room for uncertainty regarding the positioning of this view.
You should not be putting any views inside a table view, either table view cells or other views. You define cell prototypes and then the table view creates and places cells as needed based on the data source.

Align two labels in a table cell?

I am having pretty crazy amounts of issues trying to align two labels inside a table cell:
The problem is I need both labels (the region is clickable) to be centred within their cell. I can only seem to get the layout centred based one or the other.
Is this too much to expect from autolayout?
Embed the labels in a new view, then centre that view with autolayout. Also, let this new view resize itself according to the labels
So:
- Add leading and trailing constraints between labels and their superview (new view)
- Centre the new view in the UITableViewCell
I just got home, so I opened up a fresh project, with good news :)
So I set up what I described earlier, 2 labels inside an UIView
Select the labels and go to the Size Inspector, there check 'Explicit' and in the 'Preferred Width' add the Maximum - make it the maximum it is allowed to be
Now in code, you can set the labels and they auto-resize!

Layout TableView with navigation bar and tool bar

I placed TableView on a middle of a layout with constraints to stay aligned with top/bottom guide bars and left/right sides.
When I insert prototype cell it doesn't go to the top and it's the same at run time. Seems like TableView holds space on top for something?
How do I make first cell to appear on top?
In your storyboard, adjust scroll view insets of your view controller is selected by default.
The best way to solve this problem is to add top and bottom constrains between the table view and its super view, instead of the layout guide, because the view controller will adjust the insects of the table view automatically.
By default, iOS adds a content inset on the UITableView. There are two solutions. You could either set the layout constraints to the top and bottom of the view instead of the top and bottom layout guides, or you could click on the view controller and in the attributes inspector, disable Extend Edges for Under Top Bars and Under Bottom Bars.

Resources