【问题标题】:Xcode - Cannot set UITextView background color (device in dark mode)Xcode - 无法设置 UITextView 背景颜色(设备处于黑暗模式)
【发布时间】:2021-12-28 08:23:35
【问题描述】:

我试图强制 UITextView 保持白色的背景颜色,而不管设备上设置的模式如何。目前,当设备设置为浅色模式时,UITextView 的背景为白色,如果用户更改为深色模式,则 UITextView 会变为黑色(可能是预期的行为)。

无论设备上设置的模式如何,我都想强制 UITextView 的背景颜色为白色。

我已经尝试在 Xcode 的 UI 和代码中到处更改它,但它似乎不尊重颜色集并且总是将背景颜色更改为黑色。

这里是源代码:

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

这是暗模式下的样子:

这是灯光模式:

我希望将背景永久设置为白色。

【问题讨论】:

    标签: xcode uitextview


    【解决方案1】:

    我能够通过添加以下内容来解决此问题:

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

    由于某种原因,我无法通过 UI 实现它,不得不以编程方式完成。

    【讨论】:

      【解决方案2】:

      我可以通过以下方式实现这一点

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

      注意 - 我已经截断了 XML 中不相关的部分

      【讨论】:

      • 感谢您的回复,您的代码看起来与我的相似,我做了一些小改动,但我仍然遇到同样的问题。我只是无法弄清楚为什么它不起作用。我在我的问题中添加了一张图片。
      猜你喜欢
      • 2010-11-18
      • 2021-09-13
      • 1970-01-01
      • 2012-06-04
      • 2014-03-29
      • 2021-12-04
      • 2023-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多