简体   繁体   中英

iOS 9 UITableView cell's text label not the full width of the UITableView

Since updating to iOS 9, table view cell's in iPad landscape no longer stretch the full width of the table in my unit tests.

My test is a simple table that takes a snapshot at the end.

- (void)testTableSize{
    UIViewController *viewController = [[UIViewController alloc] init];
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,viewController.view.bounds.size.width, viewController.view.bounds.size.height) style:UITableViewStylePlain];

    tableView.dataSource = self;
    tableView.delegate = self;

    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [viewController.view addSubview:tableView];

    ASCSnapshotVerifyViewiPad([viewController view]);
}

The cells are very simple for this example

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...
cell.textLabel.backgroundColor = [UIColor blueColor];   // Demo purpose
cell.textLabel.backgroundColor = [UIColor yellowColor]; // Demo purpose
cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", (int)indexPath.row];
return cell;

but my cell is drawn with a big margin on the left and right. Any idea why?

在此输入图像描述

When I tested my App with iOS9, I noticed huge margins, on some UITableViews, both left and right. After a bit of investigation, I found the following new method:

// iOS9
if([self.tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)]) {
    self.tableView.cellLayoutMarginsFollowReadableWidth = NO;
}

When the above code is called, after instantiating your UITableView, it should remove them.

Paste this code after you set your tableView delegate.

Hope it helps.

hope this help you

    //    MARK: - TableView Delegates And Datasource Method -

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    // IOS 7
    if(self.respondsToSelector(Selector("setSeparatorInset:"))){
        self.separatorInset = UIEdgeInsetsZero
        cell.separatorInset = UIEdgeInsetsZero
        tableView.separatorInset = UIEdgeInsetsZero;
    }
    // OTHER
    if(self.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:"))){
        if #available(iOS 8.0, *) {
            self.separatorInset = UIEdgeInsetsZero
            cell.layoutMargins = UIEdgeInsetsZero;
            cell.preservesSuperviewLayoutMargins = false
        }
    }

    // iOS 8
    if(self.respondsToSelector(Selector("setLayoutMargins:"))){
        if #available(iOS 8.0, *) {
            self.separatorInset = UIEdgeInsetsZero
            cell.layoutMargins = UIEdgeInsetsZero
        }
    }
    // iOS 9
    if (self.respondsToSelector("setCellLayoutMarginsFollowReadableWidth:")){
        if #available(iOS 9.0, *) {
            self.cellLayoutMarginsFollowReadableWidth = false
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM