Xcode 7 iOS 9 UITableViewCell分隔符插入问题

这不是一个问题,而是解决我面临的问题。

在Xcode 7中,当应用程序在iPad设备上的iOS 9上运行时,UITableView单元格会在表格视图的左侧留下一些空白。 而将设备旋转到风景会增加利润。

我find的解决scheme是:

将“cellLayoutMarginsFollowReadableWidth”设置为NO。

self.tbl_Name.cellLayoutMarginsFollowReadableWidth = NO; 

因为,这个属性只在iOS 9中可用。所以,你将不得不放置一个条件来检查iOS版本,否则会崩溃。

 if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_8_1) { self.tbl_Name.cellLayoutMarginsFollowReadableWidth = NO; } 

希望对别人有帮助。

针对iOS 9及以上的最佳解决scheme

这是因为一个称为可读内容指南的新function。 它提供适合阅读的利润空间。 所以,在iPhone和纵向iPad上它们的利润率非常小,但是在iPad上它们更大。 在iOS 9中,UITableView的单元格边距默认为遵循可读内容指南。

如果你想停止,只需将tableView的cellLayoutMarginsFollowReadableWidth设置为NO/false

来源: https //forums.developer.apple.com/thread/5496

完美的解决scheme达到iOS 9

在viewDidLoad中

Objective-C的

 - (void)viewDidLoad { [super viewDidLoad]; //Required for iOS 9 if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 9.0) { self.testTableView.cellLayoutMarginsFollowReadableWidth = NO; } } 

迅速

  override func viewDidLoad() { super.viewDidLoad() if #available(iOS 9.0, *) { tableViewDiet.cellLayoutMarginsFollowReadableWidth = false } } 

在TableViewDelegate方法中添加以下代码:

Objective-C的

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { // Remove seperator inset if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } // Prevent the cell from inheriting the Table View's margin settings if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) { [cell setPreservesSuperviewLayoutMargins:NO]; } // Explictly set your cell's layout margins if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } } 

迅速

 func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { // Remove seperator inset if cell.respondsToSelector(Selector("setSeparatorInset:")) { cell.separatorInset = UIEdgeInsetsZero } // Prevent the cell from inheriting the Table View's margin settings if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) { cell.preservesSuperviewLayoutMargins = false } // Explictly set your cell's layout margins if cell.respondsToSelector(Selector("setLayoutMargins:")) { cell.layoutMargins = UIEdgeInsetsZero } } 

有点晚了 我希望对别人有帮助

 if #available(iOS 9.0, *) { myTableView.cellLayoutMarginsFollowReadableWidth = false } 

readableContentGuide是已经添加到每个UIView的布局指南

这是为了确保用户不必转头去阅读内容。

如果您想遵循可读的内容指南,请执行以下操作:

 let baseSection = UIView() contentView.addSubview(baseSection) baseSection.translatesAutoresizingMaskIntoConstraints = false let insets = UIEdgeInsets(top: 4, left: 0, bottom: 4, right: 0) baseSection.leadingAnchor.constraint(equalTo: readableContentGuide.leadingAnchor, constant: insets.left).isActive = true baseSection.trailingAnchor.constraint(equalTo: readableContentGuide.trailingAnchor, constant: -insets.right).isActive = true baseSection.topAnchor.constraint(equalTo: contentView.topAnchor, constant: insets.top).isActive = true baseSection.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -insets.bottom).isActive = true 

注意:在上面的代码底部和顶部的锚点使用contentView而不是readableContentGuide内容指南,以便内容的垂直边距基于tableView.rowHeight改变。