Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel in UITableViewCell in .xib file ignores Dark Mode

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?

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

enter image description here

like image 589
patrickS Avatar asked Aug 20 '19 05:08

patrickS


2 Answers

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.

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

Original answer: This original answer addresses the issue only for the default (i.e. primary) label color

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.

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>

to:

<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.

like image 83
Andrew Bennet Avatar answered Oct 03 '22 02:10

Andrew Bennet


Similar problem here. Some dynamic colors used in XIBs do not use their dark version when dark mode is enabled.

This happens both in the Simulator and on real devices. Colors do appear correctly when simulating dark mode within Xcode (Interface Builder).

My project deployment target is iOS 10.

like image 34
Loïc Gardiol Avatar answered Oct 03 '22 02:10

Loïc Gardiol