How to set the full width of separator in UITableView

前端 未结 16 793
再見小時候
再見小時候 2020-11-30 19:50

I have a UITableView where the separators don\'t have the full width. It ends like 10 pixels before the left side. I was playing around with this code in the

相关标签:
16条回答
  • 2020-11-30 20:40

    I got the answer from this post: iOS 8 UITableView separator inset 0 not working

    Just add this code on your UITableViewController

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
    
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
    }
    
    -(void)viewDidLayoutSubviews
    {
        [super viewDidLayoutSubviews];
        if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
            [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        }
    
        if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        }
    }
    
    0 讨论(0)
  • 2020-11-30 20:46

    I inherit from UITableViewController and needed additional to the two insets settings in willDisplayCell also to set preservesSuperviewLayoutMargins to false. In Swift it looks like this:

    override func tableView(_tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    
        if cell.respondsToSelector("setSeparatorInset:") {
            cell.separatorInset = UIEdgeInsetsZero
        }
        if cell.respondsToSelector("setLayoutMargins:") {
            cell.layoutMargins = UIEdgeInsetsZero
        }
        if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
            cell.preservesSuperviewLayoutMargins = false
        }
    }
    
    0 讨论(0)
  • 2020-11-30 20:47

    Tested for iOS 9.3 & Swift 2.2. Make sure to put the code in willDisplayCell which is called just before displaying the cell and not in cellForRowAtIndexPath where you create the cell only.

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        cell.separatorInset = UIEdgeInsetsZero
        cell.layoutMargins = UIEdgeInsetsZero
    }
    

    Add override to the function for UITableViewController, like so:

    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    
    0 讨论(0)
  • 2020-11-30 20:49

    Use it in cellForRowAtIndexPath method, to configure cell's separator specs ,
    it works perfect for iOS9.0+

     cell.separatorInset = UIEdgeInsetsZero;
     cell.layoutMargins = UIEdgeInsetsZero;
     cell.preservesSuperviewLayoutMargins = NO;
    
    0 讨论(0)
  • 2020-11-30 20:49

    None of the above worked for me in Swift 2.2 and Xcode 7.3.1

    Turned out to be the simplest solution of all. No code needed. Just change TableViewCell Layout Margins values in your UITableView inspector:

    0 讨论(0)
  • 2020-11-30 20:51

    for Swift 3 :

    override func viewDidLoad() {
      super.viewDidLoad()
    
      tableView.separatorInset = .zero
      tableView.layoutMargins = .zero
    }
    
    0 讨论(0)
提交回复
热议问题