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

SwiftUI中隐藏TextEditor滚动指示器:SwiftUI修饰符与UIKit方案

如何隐藏SwiftUI TextEditor的滚动指示器

一、SwiftUI原生修饰符方案(iOS 16+)

从iOS 16开始,SwiftUI提供了直接控制滚动指示器的原生修饰符 scrollIndicators(_:),无需依赖UIKit就能实现隐藏效果。

直接在你的代码中添加该修饰符即可:

TextEditor(text: $context)
    .foregroundColor(self.context == contextPlaceholder ? Color.grayA7 : Color.gray23)
    .font(.bodyRegular)
    .focused($isTextFieldsFocused)
    .background(Color.grayF5)
    .lineSpacing()
    .padding(.vertical, 23)
    .padding(.horizontal, 20)
    .textSelection(.disabled)
    .scrollIndicators(.hidden) // 新增此行隐藏滚动指示器
    .onTapGesture {
        if self.context == contextPlaceholder{
            self.context = ""
        }
    }

如果需要更灵活的控制,还可以指定隐藏时机,比如仅在非滚动状态下隐藏:scrollIndicators(.hidden, while: .notScrolling)。

二、基于UIKit的兼容方案(iOS 13+)

若需兼容iOS 16以下版本,要通过UIViewRepresentable包装UITextView实现,因为早期SwiftUI的TextEditor没有直接控制滚动指示器的API。

1. 创建自定义UITextView包装组件

struct CustomTextEditor: UIViewRepresentable {
    @Binding var text: String
    var placeholder: String
    
    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
        textView.font = UIFont.systemFont(ofSize: 17) // 对应原代码的.bodyRegular,可按需调整
        textView.textColor = UIColor(Color.grayA7)
        textView.text = placeholder
        textView.backgroundColor = UIColor(Color.grayF5)
        textView.showsVerticalScrollIndicator = false // 隐藏垂直滚动指示器
        textView.showsHorizontalScrollIndicator = false // 隐藏水平滚动指示器
        textView.textContainerInset = UIEdgeInsets(top: 23, left: 20, bottom: 23, right: 20) // 还原原padding设置
        textView.delegate = context.coordinator
        return textView
    }
    
    func updateUIView(_ uiView: UITextView, context: Context) {
        if uiView.text != placeholder && text != uiView.text {
            uiView.text = text
            uiView.textColor = text.isEmpty ? UIColor(Color.grayA7) : UIColor(Color.gray23)
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(parent: self)
    }
    
    class Coordinator: NSObject, UITextViewDelegate {
        let parent: CustomTextEditor
        
        init(parent: CustomTextEditor) {
            self.parent = parent
        }
        
        func textViewDidBeginEditing(_ textView: UITextView) {
            if textView.text == parent.placeholder {
                textView.text = ""
                textView.textColor = UIColor(Color.gray23)
            }
        }
        
        func textViewDidEndEditing(_ textView: UITextView) {
            if textView.text.isEmpty {
                textView.text = parent.placeholder
                textView.textColor = UIColor(Color.grayA7)
            }
            parent.text = textView.text
        }
        
        func textViewDidChange(_ textView: UITextView) {
            parent.text = textView.text
        }
    }
}

2. 在视图中替换原TextEditor

用自定义组件替代原来的TextEditor,即可实现隐藏滚动指示器的效果:

CustomTextEditor(text: $context, placeholder: contextPlaceholder)
    .focused($isTextFieldsFocused)
    .textSelection(.disabled) // 如需禁用文本选择,也可在自定义组件内设置textView.isSelectable = false

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 20:20:19