14

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

Nirav Kotecha
  • 2,493
  • 1
  • 12
  • 26
patrickS
  • 3,590
  • 4
  • 29
  • 40
  • 1
    I have the same problem, but it seems to extend to all nib files (not just UITableViewCells). Is that your experience too? It works fine if the deployment target is iOS13, but if you bring it down to iOS12, it stops working correctly. – Z S Aug 21 '19 at 01:01
  • 1
    Weird that the background dynamic color works correctly, but the label ones do not. This is a bit of a blocker for me implementing dark mode... – Andrew Bennet Sep 03 '19 at 09:30

4 Answers4

13

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.

Andrew Bennet
  • 2,600
  • 1
  • 21
  • 55
  • 1
    It looks like iOS 13.1 Beta 3 and iOS 13 GM fixed the bug. I have also updated my Xcode to 11 GM. No awakeFromNib calls are required anymore. – patrickS Sep 11 '19 at 06:29
  • adding does not fix the issue for dark mode. You have to provide the color too. – AsifHabib Oct 14 '19 at 05:43
3

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.

Loïc Gardiol
  • 361
  • 3
  • 7
1

Xcode 11 GM Seed 2 which was released today seems fixed the problem.

From release note:

Fixed an issue where system colors in XIB files set to deploy before iOS 13.0 wouldn’t adapt to the system appearance at runtime. (54362252)

Alfred Woo
  • 648
  • 1
  • 6
  • 23
1

@Andrew Bennet this worked for me. Thank you. I had to implement the work around in Objective-C because this part of my application is still in Objective-C.

- (void)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
        lblTitle.textColor = UIColor.secondaryLabelColor;
    }
}
Larry Ricker
  • 199
  • 1
  • 7