Remove SeparatorInset on iOS 8 UITableView for Xco

2020-01-26 03:20发布

I found a weird white space on UITableView for iPhone 6 Simulator (iOS 8) on Xcode 6 GM. I have tried to set the SeparatorInset from both storyboard and also the code, but the white space is till there.

The following code works on iOS 7 but not on iOS 8 (iPhone 6 simulator).

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

I attached screenshot below:

iPhone 6 Simulator weird white space on tableview

I am using AutoLayout by the way. I hope someone can show me a way to remove the weird white space on the TableView.

15条回答
迷人小祖宗
2楼-- · 2020-01-26 03:49

Workaround for iOS 7 & iOS 8

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    cell.separatorInset = UIEdgeInsetsMake(0.0f, cell.frame.size.width, 0.0f, 0.0f); 
}
查看更多
可以哭但决不认输i
3楼-- · 2020-01-26 03:50

To make your UITableView separator insets to zero for both iOS7 and iOS8, instead of making a change in the code, make a change in the xib for the UITableView by changing the View->Mode->Aspect Fill.

查看更多
再贱就再见
4楼-- · 2020-01-26 03:51

for iOS 8

try by setting cell.layoutMargins = UIEdgeInsetsZero; in cellForRowAtIndexPath method

查看更多
虎瘦雄心在
5楼-- · 2020-01-26 03:52

I have static UITableView and wanted to shift the margin of a cell separator to the left edge.

Thanks to the above answers, this was my solution

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    // needed to shift the margin a specific cell to the left edge
    if indexPath.section == 3 && indexPath.row == 0 {
        cell.layoutMargins = UIEdgeInsetsZero
        cell.preservesSuperviewLayoutMargins = false
        cell.separatorInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)
    }
}
查看更多
欢心
6楼-- · 2020-01-26 03:55

My solution with just three lines of code:

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)row{
    //
    // ... your code ...
    //
    if ([cell respondsToSelector:@selector(preservesSuperviewLayoutMargins)]){
        cell.layoutMargins = UIEdgeInsetsZero;
        cell.preservesSuperviewLayoutMargins = false;
    }
    return cell;
}
查看更多
仙女界的扛把子
7楼-- · 2020-01-26 03:56

If you want to remove the white line, but keep separator inset as it is, just set the cell.backgroundColor to the tableView backgroundColor. Just setting the cell.contentView.backgroundColor does not make the problem disappear.

查看更多
登录 后发表回答