iOS 8 UITableView separator inset 0 not working

前端 未结 30 1403
清酒与你
清酒与你 2020-11-22 14:38

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.

相关标签:
30条回答
  • 2020-11-22 15:23

    In Swift it's slightly more annoying because layoutMargins is a property, so you have to override the getter and setter.

    override var layoutMargins: UIEdgeInsets {
      get { return UIEdgeInsetsZero }
      set(newVal) {}
    }
    

    This will effectively make layoutMargins readonly, which in my case is fine.

    0 讨论(0)
  • 2020-11-22 15:24

    Swift 3.0 example:

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        // removing seperator inset
        if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
            cell.separatorInset = .zero
        }
        // prevent the cell from inheriting the tableView's margin settings
        if cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)) {
            cell.preservesSuperviewLayoutMargins = false
        }
        // explicitly setting cell's layout margins
        if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
            cell.layoutMargins = .zero
        }
    }
    
    0 讨论(0)
  • 2020-11-22 15:24

    In a more compact way than the most voted answer...

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

    }

    0 讨论(0)
  • 2020-11-22 15:25

    Simple solution in Swift for iOS 8 with a custom UITableViewCell

    override func awakeFromNib() {
        super.awakeFromNib()
    
        self.layoutMargins = UIEdgeInsetsZero
        self.separatorInset = UIEdgeInsetsZero
    }
    

    In this way you are setting layoutMargin and separatorInset just one time instead of doing it for each willDisplayCell as most of the above answers suggest.

    If you are using a custom UITableViewCell this is the correct place to do it. Otherwise you should do it in tableView:cellForRowAtIndexPath.

    Just another hint: you don't need to set preservesSuperviewLayoutMargins = false because default value is already NO!

    0 讨论(0)
  • 2020-11-22 15:25

    In iOS8:

    Adding this to my UITableViewCell Subclass:

    - (UIEdgeInsets)layoutMargins {
        return UIEdgeInsetsZero;
    }
    

    and this to "tableView:cellForRowAtIndexPath" or "tableView:willDisplayCell":

    [editCell setSeparatorInset:UIEdgeInsetsZero];
    

    WORKED for me.

    0 讨论(0)
  • 2020-11-22 15:27

    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!)
    0 讨论(0)
提交回复
热议问题