IOS 9 UITableView ячейка текстовой метки не полная ширина UITableView

Поскольку обновление до iOS 9, ячейка таблицы в ландшафте iPad больше не растягивает всю ширину таблицы в моих модульных тестах.

Мой тест - это простая таблица, которая делает снимок в конце.

- (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]);
}

Ячейки очень просты для этого примера

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;

но моя ячейка рисуется с большим отрывом слева и справа. Любая идея почему?

введите описание изображения здесь

Ответ 1

Когда я тестировал свое приложение с iOS9, я заметил огромные поля на некоторых UITableViews, как слева, так и справа. После небольшого исследования я нашел следующий новый метод:

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

Когда вызывается вышеуказанный код, после создания экземпляра вашего UITableView он должен удалить их.

Вставьте этот код после того, как вы установите делегат tableView.

Надеюсь, что это поможет.

Ответ 2

надеюсь, что это поможет вам

    //    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
        }
    }
}