ios - UILabel 自动布局忽略 iOS 8.1 和 8.2 上的 UITableViewCell 的左边距

标签 ios swift uitableview

我需要支持 iOS 8.1 和 8.2,但我不记得如何正确设置 layoutMargins,已经搜索了很多(在我自己的旧项目、Google、SO、OpenRadar 中)。

从 iOS 8.3 开始一切正常。

谁能给我指出正确的方向?

对于自定义 Cell 及其 contentView,我在 IB 中设置了“Preserve Superview Margins”。 “跟随可读宽度”(iOS9) 关闭。

在 iPad Air 2 模拟器上测试。

iOS 8.1 和 8.2(UILabel 的左边距太小):

enter image description here

iOS 8.3 到 9.3(正确):

enter image description here

像往常一样自动布局到 contentView(刚刚添加顶部/底部 +3px):

enter image description here

试图通过代码检查 layoutMargins(在这个 Cell 类中),但它们是正确的。

这是完整的单元格类:

class TextLabelTableViewCell: UITableViewCell {

    @IBOutlet weak var customTextLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func configure(text: String?) {
        customTextLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)

        customTextLabel.text = text
        customTextLabel.accessibilityLabel = text
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        debugPrint("layoutMargins: ", layoutMargins)
        debugPrint("contentView.layoutMargins: ", contentView.layoutMargins)
        debugPrint("separatorInsets: ", separatorInset)
        /*
         "layoutMargins: " __C.UIEdgeInsets(top: 8.0, left: 16.0, bottom: 8.0, right: 16.0) - 8, 16, 8, 16
         "contentView.layoutMargins: " __C.UIEdgeInsets(top: 8.0, left: 16.0, bottom: 8.0, right: 16.0) - 8, 16, 8, 16
         "separatorInsets: " __C.UIEdgeInsets(top: 0.0, left: 16.0, bottom: 0.0, right: 0.0) - 0, 16, 0, 0
        */
    }

}

检查 Reveal 中的单元格。一切都是正确的,除了 UILabel。这是上面单元格中标签的屏幕截图。帧 X 应为 16。

enter image description here

找到这个线程:UITableViewCell with autolayout left margin different on iPhone and iPad

有趣的是对第一个和已接受的答案的评论:

So I tried it on the 8.1 simulator and indeed, the margins are too narrow, meaning that the superview's layout margins are not preserved... However this is a bug that Apple has fixed in 8.3 and somehow fixed again in iOS 9. – Jonas Zaugg

好吧,这是一个错误是显而易见的。但是你通常如何解决?那就是我忘了。为 8.0 到 8.2(UIAppearance ftw)手动设置 layoutMargin 和 separatorInsets 的每个插图?

Sankalap Yaduraj Singh 建议的解决方案,即为 leadingMargin 设置一个额外的 margin 常量,有效:

enter image description here enter image description here

我发现:如果我通过 UIAppearance 将 layoutMargin 设置为大于设备默认值(通常 iPad 上为 16,iPhone 上为 8),则会正确应用较大的边距:

// Experimental: TableViewCells Margin Bugfix for iOS 8.0 to 8.2
UITableViewCell.appearance().layoutMargins = UIEdgeInsetsMake(8, 17, 8, 16)

注意 17,而不是 iPad 上此测试的 16。

如前所述,这是一个已知的 iOS 8.1/8.2 错误。我已经知道了,问题是如何以干净的方式解决问题。

有趣的事实:如果我在 Reveal 中将 UILabel 的前导约束常量从 0 调整为 1,然后再调整回 0,则位置是正确的。看起来这个 8.1/8.2 错误是一个 AutoLayout 错误。

最佳答案

您可以在自动布局的帮助下设置左边距。

请看下图。

enter image description here

关于ios - UILabel 自动布局忽略 iOS 8.1 和 8.2 上的 UITableViewCell 的左边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36550417/

相关文章:

ios - 为 Tableview 使用服务器端图像时减少滚动延迟

ios - 等待 AFNetworking GET 请求

ios - 视频全屏在 ios Swift AVPlayer 中不起作用?

json - 如何在 swift 中从 JSON 获取特定值

ios - refreshControl 中的完成处理程序有 EXC_BAD_ACCESS 错误

ios - 选择时如何更改位于 tableVIewCell 上的 UIButton 的图像。在 swift 3

ios - 以编程方式将选项卡栏添加到 ViewController

ios - Storyboard界面构建器约束中的颜色意味着什么

ios - swift:如何显示 scrollView 的垂直指示器?

ios - Firebase crashlytics 报告我的应用程序中存在许多可用的崩溃见解