SwiftUI 中为 TextEditor 修改背景色

其实这个问题已经很简单了,在 iOS16 发布以前,TextEditor 视图要修改背景色会比较麻烦,下面放一个iOS16 以前的示例图:

SwiftUI 中为 TextEditor 修改背景色
iOS 16 之前 TextEditor 修改背景色

现在不需要这么麻烦,有原生的 api 可以直接使用了,只需要注意 TextEditor 有一个 滚动背景视图,将它隐藏后再设置 background 就直接生效了。

隐藏 滚动背景视图

.scrollContentBackground(.hidden)

完整示例

TextEditor(text: $data.title)
    .frame(maxWidth: .infinity)
    .frame(height: 150)
    .autocapitalization(.none)
    .disableAutocorrection(true)
    .speechAdjustedPitch(0.5)
    .submitLabel(.next)
    .foregroundColor(Color.gray)
    .multilineTextAlignment(.leading)
    .padding(.horizontal)
    // 隐藏滚动背景视图
    .scrollContentBackground(.hidden)
    // 设置背景
    .background(.orange)

完事

搞定了。

  本文自 https://www.codeun.com 发布,相应代码均自主编写并严格审阅和测试,完整代码中包含丰富的学习笔记和使用方式、实用技巧。
  · 如若转载,请注明出处:https://www.codeun.com/archives/1150.html ·

(0)
上一篇 2023-08-21 下午3:12
下一篇 2023-08-22

相关推荐

发表回复

登录后才能评论