0Day Forums
iOS 8 UITableView separator inset 0 not working - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Swift (https://0day.red/Forum-Swift)
+--- Thread: iOS 8 UITableView separator inset 0 not working (/Thread-iOS-8-UITableView-separator-inset-0-not-working)

Pages: 1 2 3 4 5


iOS 8 UITableView separator inset 0 not working - cheetahs304781 - 07-18-2023

I have an app where the `UITableView`'s separator inset is set to custom values - Right `0`, Left `0`. This works perfectly in `iOS 7.x`, however in `iOS 8.0` I see that the separator inset is set to the default of `15` on the right. Even though in the xib files it set to `0`, it still shows up incorrectly.

How do I remove the `UITableViewCell` separator margins?


RE: iOS 8 UITableView separator inset 0 not working - helsell244 - 07-18-2023

I believe this is the same question that I asked here:

[To see links please register here]


In **iOS 8**, there is one new property for all the objects inherit from `UIView`. So, the solution to set the `SeparatorInset` in iOS 7.x will not be able to remove the white space you see on the UITableView in iOS 8.

The new property is called "**layoutMargins**".

@property(nonatomic) UIEdgeInsets layoutMargins
Description The default spacing to use when laying out content in the view.
Availability iOS (8.0 and later)
Declared In UIView.h
Reference UIView Class Reference

![iOS 8 UITableView setSeparatorInset:UIEdgeInsetsZero setLayoutMargins:UIEdgeInsetsZero][1]

The solution:-

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}

if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}

If you set `cell.layoutMargins = UIEdgeInsetsZero;` without checking if the `layoutMargins` exists, the app will crash on iOS 7.x. So, the best way would be checking if the `layoutMargins` exists first before `setLayoutMargins:UIEdgeInsetsZero`.


[1]:



RE: iOS 8 UITableView separator inset 0 not working - sohaledvlk - 07-18-2023

Arg!!! After playing around either doing this in your `Cell` subclass:

- (UIEdgeInsets)layoutMargins
{
return UIEdgeInsetsZero;
}

or setting the `cell.layoutMargins = UIEdgeInsetsZero;` fixed it for me.


RE: iOS 8 UITableView separator inset 0 not working - kaufmann719 - 07-18-2023

Most answers are showing separator insets and layout margins being set over a variety of methods (i.e., `viewDidLayoutSubviews`, `willDisplayCell`, etc) for cells and tableviews, but I've found that just putting these in `cellForRowAtIndexPath` works great. Seems like the cleanest way.

// kill insets for iOS 8
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8) {
cell.preservesSuperviewLayoutMargins = NO;
[cell setLayoutMargins:UIEdgeInsetsZero];
}
// iOS 7 and later
if ([cell respondsToSelector:@selector(setSeparatorInset:)])
[cell setSeparatorInset:UIEdgeInsetsZero];


RE: iOS 8 UITableView separator inset 0 not working - kinshasa704 - 07-18-2023

Swift:

override func viewDidLoad() {
super.viewDidLoad()

if self.tableView.respondsToSelector("setSeparatorInset:") {
self.tableView.separatorInset = UIEdgeInsetsZero
}
if self.tableView.respondsToSelector("setLayoutMargins:") {
self.tableView.layoutMargins = UIEdgeInsetsZero
}

self.tableView.layoutIfNeeded() // <--- this do the magic
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...

if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset = UIEdgeInsetsZero
}
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins = UIEdgeInsetsZero
}

return cell
}


RE: iOS 8 UITableView separator inset 0 not working - cicindelidae210809 - 07-18-2023

Use below code snippet avoid unwanted padding issue for UITableView in IOS 8 & 7.

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

if ([tableView respondsToSelector:@selector(setSeparatorInset:)])
{
[tableView setSeparatorInset:UIEdgeInsetsZero];
}

if ([tableView respondsToSelector:@selector(setLayoutMargins:)])
{
[tableView setLayoutMargins:UIEdgeInsetsZero];
}

if ([cell respondsToSelector:@selector(setLayoutMargins:)])
{
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}


RE: iOS 8 UITableView separator inset 0 not working - greenburg240 - 07-18-2023

Let's take a moment to understand the problem before blindly charging in to attempt to fix it.

A quick poke around in the debugger will tell you that separator lines are subviews of `UITableViewCell`. It seems that the cell itself takes a fair amount of responsibility for the layout of these lines.

iOS 8 introduces the concept of *layout margins*. By default, a view's layout margins are `8pt` on all sides, and they're *inherited* from ancestor views.

As best we can tell, when laying out out its separator line, `UITableViewCell` chooses to respect the left-hand layout margin, using it to constrain the left inset.

Putting all that together, to achieve the desired inset of truly zero, we need to:

* Set the left layout margin to `0`
* Stop any inherited margins overriding that

Put like that, it's a pretty simple task to achieve:

cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;

Things to note:

* This code only _needs_ to be run once per cell (you're just configuring the cell's properties after all), and there's nothing special about when you choose to execute it. Do what seems cleanest to you.
* Sadly neither property is available to configure in Interface Builder, but you can specify a user-defined runtime attribute for `preservesSuperviewLayoutMargins` if desired.
* Clearly, if your app targets earlier OS releases too, you'll need to avoid executing the above code until running on iOS 8 and above.
* Rather than setting `preservesSuperviewLayoutMargins`, you _can_ configure ancestor views (such as the table) to have `0` left margin too, but this seems inherently more error-prone as you don't control that entire hierarchy.
* It would probably be slightly cleaner to set only the left margin to `0` and leave the others be.
* If you want to have a 0 inset on the "extra" separators that `UITableView` draws at the bottom of plain style tables, I'm guessing that will require specifying the same settings at the table level too (haven't tried this one!)


RE: iOS 8 UITableView separator inset 0 not working - ureometer151327 - 07-18-2023

Instead of updating `preservesSuperviewLayoutMargins` and `layoutMargins` every time the cell scrolls in (using `willDisplayCell`), I'd suggest to do it once in `cellForRowAtIndexPath:`:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)

cell.preservesSuperviewLayoutMargins = false
cell.layoutMargins = UIEdgeInsetsZero

return cell

}


RE: iOS 8 UITableView separator inset 0 not working - emetrek - 07-18-2023

let do as my code:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}

if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}

if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
cell.preservesSuperviewLayoutMargins = NO;
[cell setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
[cell setSeparatorInset:UIEdgeInsetsZero];
}
}


RE: iOS 8 UITableView separator inset 0 not working - input634 - 07-18-2023

I made it work by doing this:

tableView.separatorInset = UIEdgeInsetsZero;
tableView.layoutMargins = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;