Home > Software engineering >  Xcode - Cannot set UITextView background color (device in dark mode)
Xcode - Cannot set UITextView background color (device in dark mode)

Time:11-18

I am trying to force a UITextView to keep the background color of white regardless of the mode set on the device. At the moment, when the device is set in light mode, the background of the UITextView is white, if the user changes to dark mode, the UITextView changes to black (probably the expected behaviour).

I want to force the background color for UITextView to white regardless of the mode set on the device.

I have tried changing this everywhere both in the UI of Xcode and in the code, but it does not seem to respect the color set and always changes the background colour to black.

Here is the source code:

<view clipsSubviews="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="yI7-hV-e1x">
                                                <rect key="frame" x="20" y="139" width="335" height="150"/>
                                                <subviews>
                                                    <textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="zzp-UV-fkE">
                                                        <rect key="frame" x="0.0" y="0.0" width="335" height="150"/>
                                                        <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                                                        <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                                        <textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
                                                        <userDefinedRuntimeAttributes>
                                                            <userDefinedRuntimeAttribute type="string" keyPath="placeholder" value="Enter message here"/>
                                                            <userDefinedRuntimeAttribute type="color" keyPath="background.Color">
                                                                <color key="value" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                                                            </userDefinedRuntimeAttribute>
                                                            <userDefinedRuntimeAttribute type="color" keyPath="color">
                                                                <color key="value" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                                                            </userDefinedRuntimeAttribute>
                                                            <userDefinedRuntimeAttribute type="color" keyPath="UITextView.backgroundColor">
                                                                <color key="value" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                                                            </userDefinedRuntimeAttribute>
                                                        </userDefinedRuntimeAttributes>
                                                    </textView>
                                                </subviews>
                                                <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                                                <constraints>
                                                    <constraint firstAttribute="trailing" secondItem="zzp-UV-fkE" secondAttribute="trailing" id="3r4-ih-LvH"/>
                                                    <constraint firstAttribute="bottom" secondItem="zzp-UV-fkE" secondAttribute="bottom" id="43N-Io-MuA"/>
                                                    <constraint firstItem="zzp-UV-fkE" firstAttribute="leading" secondItem="yI7-hV-e1x" secondAttribute="leading" id="FUG-Np-Cu1"/>
                                                    <constraint firstItem="zzp-UV-fkE" firstAttribute="top" secondItem="yI7-hV-e1x" secondAttribute="top" id="r6c-Py-iV8"/>
                                                    <constraint firstAttribute="height" constant="150" id="xlc-gd-Q43"/>
                                                </constraints>
                                                <userDefinedRuntimeAttributes>
                                                    <userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
                                                        <integer key="value" value="7"/>
                                                    </userDefinedRuntimeAttribute>
                                                    <userDefinedRuntimeAttribute type="number" keyPath="layer.borderWidth">
                                                        <integer key="value" value="1"/>
                                                    </userDefinedRuntimeAttribute>
                                                    <userDefinedRuntimeAttribute type="color" keyPath="layer.borderColor">
                                                        <color key="value" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                                                    </userDefinedRuntimeAttribute>
                                                    <userDefinedRuntimeAttribute type="color" keyPath="background.Color">
                                                        <color key="value" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                                                    </userDefinedRuntimeAttribute>
                                                </userDefinedRuntimeAttributes>
                                            </view>

This is what it looks like in dark mode:

enter image description here

And this is Light Mode:

enter image description here

I want the background permanently set to white.

CodePudding user response:

I can achieve that with the following

<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" text="Reason for rejection" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="pzd-Bi-M3G">
                                        ...
                                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                                        ...
                                        <color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                                        ...
                                    </textView>

Note - I've truncated the irrelevant bits of XML

CodePudding user response:

I was able to resolve this by adding the following:

UITextView.appearance.backgroundColor = [UIColor whiteColor];
UITextView.appearance.textColor = [UIColor blackColor];

For some reason I could not achieve it via the UI and had to do it programatically.

  • Related