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

SwiftUI中TextEditor背景颜色无法修改问题咨询

TextEditor背景设置不生效的原因及解决办法

你遇到的问题是因为SwiftUI的TextEditor底层基于UIKit的UITextView实现,默认自带一层白色的系统背景视图,直接通过.background()添加的颜色会被这层系统背景遮挡,所以看不到设置的橙色。

解决方法很简单,只需要先将TextEditor的背景设为透明,再添加自定义背景色即可:

ZStack(alignment: .leading ) {
    Color.black.ignoresSafeArea()
    TextEditor(text: $mytext)
        .background(Color.clear) // 清除系统默认的白色背景
        .background(Color.orange) // 设置自定义橙色背景
        .padding()
        .foregroundColor(Color.gray)
        .frame(width: 362, height: 400)
}

如果需要更精细的样式控制,也可以通过UIViewRepresentable封装自定义的UITextView,不过上述方法已经能解决大部分场景下的背景色设置需求。

内容的提问来源于stack exchange,提问作者Steven-Carrot

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 15:09:12