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
相关产品推荐
相关产品推荐

