You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xcode 设备深色模式下无法设置UITextView背景为白色的问题

强制UITextView无视系统显示模式保持白色背景

问题说明

需要强制UITextView无论设备处于浅色/深色模式,都固定显示白色背景。常规的Xib背景色设置不生效,深色模式下会自动变为系统默认的黑色背景。

相关Xib源码

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

现象截图

  • 深色模式下效果:深色模式显示效果
  • 浅色模式下效果:浅色模式显示效果

解决方案

设置不生效的核心原因是iOS13及以上版本系统会自动适配深色模式,你使用的通用灰度色彩空间的白色属于动态颜色,会在深色模式下被系统替换为暗色。按以下任意方案修改即可:

方案1:Xib设置

  1. 选中目标UITextView,删除所有你之前添加的background.Color、UITextView.backgroundColor这类冗余的用户定义运行时属性,避免配置冲突
  2. 在右侧属性检查器找到Background属性,打开颜色选择器,切换到RGB滑块模式,手动设置R=255、G=255、B=255、A=1,颜色空间选择为sRGB IEC61966-2.1即可

方案2:代码设置

在对应ViewController的viewDidLoad生命周期方法中添加代码,手动指定固定色值:

Swift 代码

// 固定白色背景
textView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
// 可选:固定文字为黑色,避免深色模式下文字自动变为白色
textView.textColor = .black

Objective-C 代码

// 固定白色背景
self.textView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
// 可选:固定文字为黑色,避免深色模式下文字自动变为白色
self.textView.textColor = [UIColor blackColor];

兜底方案

如果上述方案都未生效,可以直接强制关闭当前页面的深色模式适配:

Swift 代码

override func viewDidLoad() {
    super.viewDidLoad()
    if #available(iOS 13.0, *) {
        // 强制当前页面始终使用浅色模式,所有控件都不会适配深色模式
        self.overrideUserInterfaceStyle = .light
    }
}

Objective-C 代码

- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 13.0, *)) {
        // 强制当前页面始终使用浅色模式,所有控件都不会适配深色模式
        self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
}

内容的提问来源于stack exchange,提问作者omega1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 11:45:03