How to set the full width of separator in UITableView

前端 未结 16 792
再見小時候
再見小時候 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:29

    The Separator Inset is by default 15 from left. Change Separator Inset option from auto to custom and set the inset to 0.

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

    In viewDidLoad (tested iOS11 - swift 4.1)

    try

    tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)

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

    For people having issues with the iPad — this will get you in a state that is the same as the iPhone. You can then adjust the separatorInset as needed.

    tableView.cellLayoutMarginsFollowReadableWidth = false
    
    0 讨论(0)
  • 2020-11-30 20:38

    None of these solutions work on the iPad, but I have come up with a solution that covers both devices:

    With reusable cells:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        ...[other code]...
        [cell setLayoutMargins:UIEdgeInsetsZero];
        [cell setSeparatorInset:UIEdgeInsetsZero];
        return cell;
    }
    

    With non reusable cells:

    - (void)removeSeparatorInset:(UITableView*)tableView{
        NSArray *cells = [tableView visibleCells];
        for (UITableViewCell *cell in cells){
            [cell setLayoutMargins:UIEdgeInsetsZero];
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
    }
    
    -(void) viewDidLayoutSubviews{
       [super viewDidLayoutSubviews];
       [self removeSeparatorInset:self.tableView];
    }
    

    Just to expand on this approach:

    @property(nonatomic) UIEdgeInsets separatorInset;
    @property(nonatomic) UIEdgeInsets layoutMargins;
    

    Both properties can be used by UITableView & UITableViewCell. The latter is, in fact, a property of UIView, which is a parent class of both UITableView & UITableViewCell.

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

    In your UITableViewCell

    Go to Attributes Inspector in your Interface Builder and simply change "15" to 0. Do this for all the cells you wish to change.

    You may need to add [cell setLayoutMargins:UIEdgeInsetsZero]; to your tableViewCell

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

    This worked for me on iOS 8.4 - 9.0 devices using Xcode 6.4 and Swift 1.2:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = UITableViewCell()
    
    
        cell.preservesSuperviewLayoutMargins = false
        cell.separatorInset = UIEdgeInsetsZero
        cell.layoutMargins = UIEdgeInsetsZero
    
        return cell
    }
    

    Swift 5 Update:

    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsets.zero
    cell.layoutMargins = UIEdgeInsets.zero
    
    0 讨论(0)
提交回复
热议问题