【问题标题】:Xcode 7 iOS 9 UITableViewCell Separator Inset issueXcode 7 iOS 9 UITableViewCell 分隔符插入问题
【发布时间】:2015-10-10 17:42:42
【问题描述】:

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

在 Xcode 7 中,当应用程序在 iPad 设备上的 iOS 9 上运行时,UITableViewCell 会在UITableView 的左侧留下一些空白。并且将设备旋转到横向会增加边距。

我找到的解决办法是:

cellLayoutMarginsFollowReadableWidth 设置为NO

self.tbl_Name.cellLayoutMarginsFollowReadableWidth = NO;

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

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

【问题讨论】:

  • 是的,这非常适合我,尤其是在装有 iOS 9 的 iPad 上。
  • 这个答案我刚遇到一个严重的问题,它使用版本NSFoundationVersionNumber_iOS_8_1,它需要使用NSFoundationVersionNumber_iOS_8_4,否则运行iOS8.2和iOS8.3和IOS8的应用会崩溃.4
  • 我使用float version = [[[UIDevice currentDevice] systemVersion] floatValue]; 获取我的系统版本,以便您可以轻松地与if (version >= 9.0) 进行比较(从而防止由于同一iOS 版本的进一步更新而导致潜在的崩溃)
  • 请参阅stackoverflow.com/questions/32845075/… 以获得更好的方法,检测功能而不是 iOS9 版本号

标签: objective-c uitableview xcode7 ios9


【解决方案1】:

iOS 9 及更高版本:

这是因为一项称为可读内容指南的新功能。它提供适合阅读的边距。因此,在 iPhone 和纵向 iPad 上,它们的利润非常小。但在景观方面,iPad 他们更大。在 iOS 9 中,UITableViewCell 边距默认遵循可读内容指南。

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

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

【讨论】:

  • 非常好的答案!遗憾的是尚未记录。这个问题发生在我准备mine
【解决方案2】:

最高 iOS 9

在 viewDidLoad 中

目标-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 方法中添加以下代码:

目标-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
    }
}

【讨论】:

  • 这对我不起作用。这就是为什么我不得不寻找其他方法。
  • 这不适用于 iOS 9。您是否使用 iOS 9 和 Xcode 7 在首选项中正确设置了命令行工具对其进行了测试?
  • 如果你使用 swift 你应该使用if #available(iOS 9.0, *) 条件检查。
【解决方案3】:

我希望这会有所帮助。

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

【讨论】:

    【解决方案4】:

    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 而变化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 2015-10-10
      • 2013-10-08
      • 2023-04-09
      相关资源
      最近更新 更多