Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 448 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iOS 8 UITableView separator inset 0 not working

#1
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?
Reply

#2
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]:
Reply

#3
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.
Reply

#4
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];
Reply

#5
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
}
Reply

#6
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];
}
}
Reply

#7
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!)
Reply

#8
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

}
Reply

#9
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];
}
}
Reply

#10
I made it work by doing this:

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

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through