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

stu*_*uti 68 objective-c uitableview ios9 xcode7

这不是一个问题,而是我面临的问题的解决方案.

在Xcode 7中,当应用程序在iPad设备上的iOS 9上运行时,UITableView单元格会在tableview的左侧留下一些边距.将设备旋转到横向会增加边距.

我找到的解决方案是:

将"cellLayoutMarginsFollowReadableWidth"设置为NO.

self.tbl_Name.cellLayoutMarginsFollowReadableWidth = NO;
Run Code Online (Sandbox Code Playgroud)

因为,此属性仅在iOS 9中可用.因此,您必须设置条件来检查iOS版本,否则它将崩溃.

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_8_1)
{
    self.tbl_Name.cellLayoutMarginsFollowReadableWidth = NO;
}
Run Code Online (Sandbox Code Playgroud)

希望它对其他人有所帮助.

Viz*_*llx 39

iOS 9及更高版本的最佳解决方案

这是因为一项称为可读内容指南的新功能.它提供适合阅读的边距.因此,在iPhone和人像iPad中,它们的利润非常小,但在景观iPad中它们更大.在iOS 9中,UITableView的单元格边距默认遵循可读的内容指南.

如果你想停止它,只需将tableView设置UITableViewCellcellLayoutMarginsFollowReadableWidth.

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

  • 非常好的答案!遗憾的是它尚未记录在案.那个问题发生在我准备[我的]时(http://stackoverflow.com/questions/31537196/ios-9-uitableview-separators-insets-significant-left-margin/) (2认同)

Bhu*_*att 13

适用于iOS 9的完美解决方案

在viewDidLoad中

Objective-C的

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

迅速

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

在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];
    }
}
Run Code Online (Sandbox Code Playgroud)

迅速

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
    }
}
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

12107 次

最近记录:

6 年,2 月 前