Add space between tableviewcell and tableview - ios

What's the proper way to set left an right margins for a tableviewcell within a tableview?
Here's an image of what Im going for:
And here's what I actually have:
I can get the spacing on the left, but not on the right. I've set the contentInsent to this:
self.tableView.contentInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
but it's not working either. Instead it's allowing a horizontal scroll on the tableview.

If I wanted to achieve the interface you're showing, this would have nothing to do with the table view insets. I would draw the cells to have that appearance. This would be a matter of the cell's background view. Each cell would fill the space, as intended, but the cell would draw an inner rounded rect within itself.

Related

Inconsistent tableView contentInset Behaviour

I am experiencing issues with setting contentInset on a tableView behaving inconsistently, depending on when it is called. I am using the following:
let edgeInsets = UIEdgeInsets(top: 52, left: 0, bottom: 0, right: 0)
tableView.contentInset = edgeInsets
tableView.scrollIndicatorInsets = edgeInsets
The top value is hard set to 52 there for testing, but in practice will be calculated from another view.
I am doing this because I need to have a view pinned to the top of a table view controller.
The behaviour I am experiencing is that if I use that code in the viewDidLoad or viewWillAppear functions, it works as I would expect. The tableView is below the pinned view and scrolled to its top (so all its content is visible). However, at that point, the pinned view has not been laid out, and its height is 0, so I can't use it to correctly set the top of the content inset (hence the hard 52).
If I use the above code from the viewDidLayoutSubviews function, which is where I have had it the whole time, it does not have the same results. The insets are actually set, but the tableView is also scrolled that much down, hiding its top rows behind the pinned view. I can then scroll up to see the top cells, and the tableView is then inset under the header, but it shouldn't start scrolled like that, and I have no idea why it is.

Last cell on UITableView cut off. Solved adding one point as contentInset: iOS bug?

I have a UITableView where the last cell is cut off behind the UITabBarController. I set the bottom constraint of the tableView to the top of my bottomLayoutGuide. I also tried this approach without success.
The only solution that worked was adding insets to the content (one point was enough):
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 1, right: 0)
Therefore I wonder if that's a normal behaviour or a scroll view bug.

Floating UIButton over UICollectionView

I have a UIButton, which is fixed in position, over a UICollectionView, not part of the actual UICollectionView view hierarchy.
Is there a way I am able to, on vertical scroll of the UICollectionView, to dynamically adapt the scroll length where the last row of the UICollectionView always appears above the floating button?
Haven't found anything to reference and am admittedly lost on implementation. Hope for any thoughts..
Below is the desired functionality:
You can make it so that your UICollectionView scrolls above your button by setting a contentInset to account for the height of your button (plus the buffer above and below the button. That could look something like this:
collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: button.bounds.size.height + buffer, right: 0)
"buffer" is a placeholder for whatever you want to add to account for the space above and below the button.
If I understood your problem, you could do something like that:
yourButton.zPosition = 100
And your button should appear above your collection view.

Setting a minimum content size on a UITableView

I have a view at the top of a view controller, and a tableview underneath it.
I've made it such that as the tableview is scrolled up the top view scrolls up too, up to a maximum amount, of lets say 50 points.
The tableview also has a top inset of 50:
tableView = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)
... so that it's cells start below the top view.
And in the scrollview delegate there is some code along the lines of:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let scrollViewYOffset = ...
topViewHeightConstraint.constant = max(minTopViewHeight, minTopViewHeight - scrollViewYOffset)
}
This ensures as the user drags down on the tableview the top view 'sticks' to it, and is also pushed up when the user pushes the table view up.
I've drawn this picture to better describe what the Storyboard looks like:
So far so good. When there are a lot of cells and the user scrolls the table view up, the top view remains at it's minimum height nicely.
But if you are scrolled up - so the top view is at it's minimum - then the number of cells are reduced, the top view pings back down to it's maximum height.
This is because the actual content size of the tableview has dropped below its bounds height, and so as a scrollview it brings the top of the content to the top of the scrollview again (plus the 50 point top inset of course).
I would like to be able to scroll the tableview up, so the top view remains at its minimum height, regardless of the number of cells it contains - i.e. regardless of it's content size.
Can anyone think of a clever way to set a minimum content size on the table view?
(So far I've tried messing around with the footer, having a cell at the bottom that is essentially a spacer - this messes up the tableview's logic and some reordering code I have in there. I've attempted to coerce the offers etc. to my will, but haven't quite worked out how to achieve this.)
I would greatly appreciate some UI genius to point me in the right direction :) Thank you.
UPDATE:
Thank you for all the answers and comments.
After trying various types of footer and header views, tweaking constraints & layout priorities on scroll, adding spacer cells, putting the tableview inside a scrollview, etc. - it finally occurred to me I was making this more complicated than I needed to, and should just update the cause of the problem on the scrollViewDidScroll, the contentInset value.
See the answer below for a code example that achieves the behaviour I was looking for.
As the contentInset is what is causing the tableview to ping back to the wrong point, I simply needed to adjust the content inset as the tableview was scrolled up/down.
Here is some example code of what I did:
let maxPointsTopViewCanMoveUp: CGFloat = 50
let topInset = abs(min(max(-maxPointsTopViewCanMoveUp, scrollView.contentOffset.y), 0))
scrollView.contentInset = UIEdgeInsets(top: topInset, left: 0, bottom: 0, right: 0)
scrollView.scrollIndicatorInsets = UIEdgeInsets(top: topInset, left: 0, bottom: 0, right: 0)
let amountToMoveTopViewUp = maxPointsTopViewCanMoveUp - topInset
topViewToSuperviewTopConstraint.constant = amountToMoveTopViewUp
This is called from the scrollViewDidScroll of the tableview.
It means that when there are too few cells in the tableview to fill the content, the top the tableview sticks in the place it had been scrolled up to (i.e. the amount it had pushed up the top view).
Some ideas that you can try:
Create a "dummy" row on index 0 and make it's height be 50 (it will be hidden below the top view). Maybe you can leave that dummy row in section 0 and the rest of your data in section 1, so you don't have to think about it when deleting your data.
Or, instead of a dummy row, you can set the height of the tableView's header to be 50
Maybe set a minimum height constraint on the tableView, and keep it's content compression resistance priority high.

TableView ContentInset does not shrink cell width / Add horizontal padding to TableView contents

I'd like to have a UITableView which is full screen. But the content of the UITableView should have a padding on the left and right.
So I tried to set ContentInset. But now the cells are as wide as the UITableView and the UITableView scrolls horizontally.
Is there a way to say that the UITableView content's width should become narrowed by the horizontal content insets? Or do I have to add the padding to all cells and header/footer views?
I don't want to narrow the table view itself, because the scroll indicator should stay at the right side of the screen and not in the middle.
The here (How to set the width of a cell in a UITableView in grouped style) suggested solution seems to be not as generic as i'd love to, beacuse the cells and header and footer views have to know about the padding (at least 3 places to maintain instead of one)
I don't want to narrow the table view itself, because the scroll
indicator should stay at the right side of the screen and not in the
middle.
This makes you happy?
_tableView.clipsToBounds = NO;
_tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -30.f);
If you don't like clipsToBounds = NO effects, you can embed the tableView in container view which is clipsToBounds = YES.
Set the layout margins of the table view. For this to work make sure your constraints in the cells are set relative to the superview margin.
tableView.layoutMargins = UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 40)

Resources