iOS - standard insets for tableview cell separators - ios

as the title states, I'm wondering what the standard insets are for the default iOS tableview is. I'm using a different library and want to set the insets equal to the default table view cells.

You can use insets property given in storyboard or you can give insets programmatically.
Like,
[self.tableView setContentInset:UIEdgeInsetsMake(108, 0, 0, 0)]; // Top, Left, Right, Bottom

Related

Spacing between navigationBar and tableview cell

How can I add a spacing between navigationBar and first UITableViewCell?
You need to add contentInset to your UITableView the first value is for top inset the second is left third for bottom and the last for right
Like this
self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0) //replace 10 by your needed value
You don't need to do it from the cell, but from the tableview:
go to your storyboard where your tableview is, and change the top constraint of your tableview to be something more than the 0 that it currently is.

How to remove UITableView top inset when embedded in ContainerView?

I am currently working on an iPhone App. I now have a problem with the top spacing inset of a UITableView. See this screenshot:
There should be no space between the table view cells and the buttons.
I do not know how to fix this. The UITableView is embedded in a ContainerView like this:
I think I got the container view constraints right. Top Space to Chapter Button is set to 0.
I tried to change some settings of the table view controller in storyboard. For example the Adjust Scroll View Insets. However it does not change anything when I disable that.
I also tried to set the TableView insets directly in the code in viewDidLoad():
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
However this also did not fix it.
Can anyone help? I have no idea where to look.
This can be solved by disabling auto adjustment of scroll view insets. However this setting has to be applied to the ViewController which contains the ContainerView (with the UITableView inside of it). It does not work when applied to the UITableView itself.
This can be done via Storyboard:
Or via code in viewDidLoad(): self.automaticallyAdjustsScrollViewInsets = false
Since iOS 15 there's a new parameter which can introduce a top gap when using a section header. This can be removed with the following:
if #available(iOS 15, *) {
tableView.sectionHeaderTopPadding = 0
}
Try this Hope this work.
hide View which contains the ContainerView.
self.automaticallyAdjustsScrollViewInsets = NO

Need assistance regarding UITableViewCell SeparatorInset

1> I have UITableViewCell and UITableViewCell subclass cell both in my tableview. Setting SeparatorInset on UITableViewCell subclass cell is not working?, but If I set it on UITableViewCell it works fine.
2> Apple doc regarding SeparatorInset says:
You can use this property to add space between the current cell’s
contents and the left and right edges of the table. Positive inset
values move the cell content and cell separator inward and away from
the table edges. Negative values are treated as if the inset is set to
0.
Only the left and right inset values are respected; the top and bottom
inset values are ignored. The value assigned to this property takes
precedence over any default separator insets set on the table view.
a. If I set [cell setSeparatorInset:UIEdgeInsetsMake(0, 100, 0, 0)]; on UITableViewCell it push the content towards right
but setting [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 100)]; on UITableViewCell push only separator towards left not the content, why?
1)
In UITableViewCell you have subviews which position you cannot edit directly (title, accessoryView, ...). Here comes separatorInset which enables you to adjust position of those subviews (in other words separatorInset is considered during position calculation of those subviews).
In custom UITableViewCell subclass you are positioning your own subviews, so unless you specifically consider separatorInset during calculation of their position, it has no effect.
2)
separatorInset does not 'push' the content to left/right, rather adds (virtual) insets to container in which they are positioned. So if e.g. you only have title that does not stretch to full width of the cell (-100) you would not see any difference after adding right inset.

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)

UICollectionVIew: How can I remove the space on top first cell's row?

I have this arrangement in Interface Builder, all properties are set to zero.
However, when I run it on both device and simulator it appears like this
Where is the space above the cells come from?
So I try to set these properties for UICollectionViewFlowLayout in code like this
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.headerReferenceSize = CGSizeZero;
layout.footerReferenceSize = CGSizeZero;
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 0;
layout.itemSize = CGSizeMake(103, 119);
self.calendarView.collectionViewLayout = layout;
but I have no luck.
How can I get rid of that space?
UICollectionView is descendant of UIScrollView class which has contentInset property, setting -20 top inset fixes the problem
[self.calendarView setContentInset:UIEdgeInsetsMake(-20, 0, 0, 0)];
However, the problem comes from UIViewController's automaticallyAdjustsScrollViewInsets property.
By documentation:
Default value is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if you want to manage scroll view inset adjustments yourself.
That's why we get adjusted content insets for status bar. It's better to disable automatically adjusting than manually set value which doesn't match in result picture.
[self setAutomaticallyAdjustsScrollViewInsets:NO];
Another way is to select your ViewController and uncheck the checkbox Adjust Scroll View Insets in your interface builder:
It is essentially the same as the following line of code. But you got to see your changes right away in the interface builder.
automaticallyAdjustsScrollViewInsets = false
iOS 11 deprecated the use of automaticallyAdjustsScrollViewInsets, so the use of collectionView.contentInsetAdjustmentBehavior = .never is advised.
Here is the answer in swift with a few adjustments:
I have a collection view that takes up a small portion of the view. I used:
self.automaticallyAdjustsScrollViewInsets = false
to remove the top spacing that was messing up my layout. This piece of code didn't work for me:
self.paperCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
And neither did this one:
self.paperCollectionView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)
But that might be because I'm not using a UICollectionViewController, I'm just using a UICollectionView.
Here's a bigger portion of the code to give more context:
You can also go with
[self.calendarView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
[self setAutomaticallyAdjustsScrollViewInsets:NO];
There is by default collection view header scrolling space added on the collection view and you do not need to add -20 from top because it may reflect on device issue
You can do this in Interface Builder by going to the Scroll View section and changing the Content insets dropdown to "Never".
Swift 3:
self.automaticallyAdjustsScrollViewInsets = false
This answer is weird, but it works if you are working in Interface Builder and have a Collection View embedded in a View Controller that is under the control of a Tab Bar Controller that is the root view controller of a Navigation Controller.
Add a Toolbar to the View Controller that has the Collection View
Move the Toolbar in the hierarchy such that it is above the Collection View
If the Toolbar is above the Collection View, there will be no space from the top of the prototype Collection View Cell to the Collection View. If there is no Toolbar or the Toolbar is below the Collection View, then there will be space between the top of the Collection View and the Collection View Cell. This is true both in the Storyboard preview and while running the app. The same type of thing occurs for Table Views.
This was most recently tested with Xcode Version 8.3.3

Resources