.xib 文件中 UITableViewCell 中的 UILabel 忽略暗模式 [英] UILabel in UITableViewCell in .xib file ignores Dark Mode

查看:23
本文介绍了.xib 文件中 UITableViewCell 中的 UILabel 忽略暗模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .xib 文件中有我的自定义 UITableViewCells.由于 Xcode 11 Beta 5(也使用 Beta 6 进行了测试),我的 UILabels 忽略了暗模式并且文本始终为黑色.我已将 UILabel 文本颜色设置为默认(标签颜色),因此这应该会自动更改.有没有人知道出了什么问题?

I have my custom UITableViewCells in .xib files. Since Xcode 11 Beta 5 (also tested with Beta 6) my UILabels ignore the Dark Mode and the text is always black. I have set the UILabel text color to Default (Label Color) so this should change automatically. Does anyone have an ideas what's wrong?

这是一个屏幕截图:第一个单元格是一个基本单元格,第二个是 .xib 文件中的自定义单元格.

Here is a screenshot: The first cell is a basic cell, the second one is a custom cell in a .xib file.

推荐答案

这似乎是 Xcode 11 中的错误(在 beta 7 和 GM Seed 1 中测试) - 我已通过反馈助手 (FB7198213) 向 Apple 提交了此问题.该问题在 Xcode 11 GM Seed 2 中已修复.

This seems to be a bug in Xcode 11 (tested in beta 7 and GM Seed 1) - I have filed this issue with Apple via Feedback Assistant (FB7198213). The issue is fixed in Xcode 11 GM Seed 2.

对于先前版本的 Xcode 11,动态标签颜色不正确行为的解决方法是在表视图单元格子类中的 awakeFromNib() 中重新分配标签颜色.例如:

For previous versions of Xcode 11, a workaround for the incorrect behaviour of dynamic label colors is to reassign the label color in awakeFromNib() in the table view cell subclass. E.g.:

class TableCell: UITableViewCell {

    @IBOutlet private weak var label: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        if #available(iOS 13.0, *) {
            // The label's textColor was set to secondaryLabel in the XIB editor
            // but we reassign it to secondaryLabel again here. This prevents
            // a bug where the label always appears in its light mode variant.
            label.textColor = .secondaryLabel
        }
    }
}


原答案:此原始答案仅针对默认(即主要)标签颜色解决了该问题

Xcode 11(在 beta 7 中测试)中的 XIB 编辑器 中似乎存在与编辑标签颜色有关的错误.当在 XIB 编辑器中将标签颜色设置为标签颜色"时(即使它已经设置为该颜色),底层 XML 的修改方式会导致标签即使在暗模式下也显示为黑色.检查创建新标签和将该标签的颜色显式设置为标签颜色"之间的 XIB 文件的差异,可以看到差异.

There seems to be a bug in the XIB editor in Xcode 11 (tested in beta 7) with regards to editing label colours. When a label color is set in the XIB editor to "Label Color" (even if it was already set to that), the underlying XML is modified in a way which results in the label appearing black even in dark mode. Examining the diff of a XIB file between the creation of a new label, and after explicitly setting that label's color to "Label Color", one can see the difference.

对于我的简单示例,XIB 文件的 XML 来自:

For my simple example, the XIB file's XML went from:

<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Text here" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="VKH-gX-gtO">
   <rect key="frame" x="20" y="15" width="71" height="21"/>
   <fontDescription key="fontDescription" type="system" pointSize="17"/>
   <nil key="textColor"/>
   <nil key="highlightedColor"/>
</label>

到:

<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Text here" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="VKH-gX-gtO">
   <rect key="frame" x="20" y="15" width="71" height="21"/>
   <fontDescription key="fontDescription" type="system" pointSize="17"/>
   <nil key="highlightedColor"/>
</label>

请注意,行 已被删除.手动添加此回可修复标签在暗模式下的行为.

Note that the line <nil key="textColor"/> was removed. Manually adding this back fixes the behaviour of the label in dark mode.

这篇关于.xib 文件中 UITableViewCell 中的 UILabel 忽略暗模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆