如果我使用TextField,但不使用UITextView,工具栏会显示出来.我不确定这是一个错误,还是我没有正确地将UITextView与SwiftUI连接起来.有没有关于如何使.bar()与UITextView一起工作的 idea ?以下是我的代码:

struct MyUITextView: UIViewRepresentable {
    @Binding var text: String
    private let editor = UITextView()
    
    func makeUIView(context: Context) -> UITextView {
        editor.delegate = context.coordinator
        
        return editor
    }
    func makeCoordinator() -> Coordinator {
        Coordinator(text: $text)
    }
    
    func updateUIView(_ editor: UITextView, context: Context) {
        editor.text = text
    }
    
    class Coordinator: NSObject, UITextViewDelegate {
        @Binding private var text: String
        
        init(text: Binding<String>) {
            self._text = text
        }
        
        func textViewDidChange(_ editor: UITextView) {
            text = editor.text
        }
    }
}

struct ContentView: View {
    @State var uitextView = "UITextView"
    @State var textfield = "TextField"
    var body: some View {
        VStack {
            MyUITextView(text: $uitextView) // Toolbar doesn't show up when focused
                .border(.black, width: 1)
                .frame(maxHeight: 40)
            TextField("", text: $textfield) // Toolbar shows up when focused
                .border(.black, width: 1)
        }
        .toolbar {
            ToolbarItem(placement: .keyboard) {
                Button("Click") {}
            }
        }
    }
}

Demo

推荐答案

SwiftUItoolbar不知道UIKit视图,所以它不会在UITextView中添加工具栏.

您仍然可以使用UIKit API添加工具栏:

let toolbar = UIToolbar()
toolbar.items = [...]
editor.inputAccessoryView = toolbar
toolbar.translatesAutoresizingMaskIntoConstraints = false

如果你想使用SwiftUI来编写工具栏的内容,你必须在协调器中添加一个UIHostingController.

出于某种原因,添加单个UIBarButtonItem(customView: hostingController.view)会使用户无法按下SwiftUIButton.你have要把整个inputAccessoryView变成一个SwiftUI.

示例:

// UIViewRepresentable and Coordinator design inspired by https://stackoverflow.com/a/74788978/5133585
struct MyUITextView<Toolbar: View>: UIViewRepresentable {
    init(text: Binding<String>, @ViewBuilder toolbar: @escaping () -> Toolbar) {
        self._text = text
        self.toolbar = toolbar
    }
    
    @Binding var text: String
    let toolbar: () -> Toolbar
    
    func makeCoordinator() -> Coordinator {
        Coordinator(hostingVC: UIHostingController(rootView: toolbar()))
    }
    
    func makeUIView(context: Context) -> UITextView {
        context.coordinator.editor
    }
    
    func updateUIView(_ uiView: UITextView, context: Context) {
        uiView.text = text
         context.coordinator.stringDidChange = { string in
            text = string
        }
    }
    
    class Coordinator: NSObject, UITextViewDelegate {
        
        let hostingVC: UIHostingController<Toolbar>
        
        init(hostingVC: UIHostingController<Toolbar>) {
            self.hostingVC = hostingVC
            hostingVC.sizingOptions = [.intrinsicContentSize]
        }
        
        lazy var editor: UITextView = {
            let editor = UITextView()
            editor.delegate = self
            editor.inputAccessoryView = hostingVC.view
            editor.inputAccessoryView?.translatesAutoresizingMaskIntoConstraints = false
            return editor
        }()
        
        var stringDidChange: ((String) -> ())?
        
        func textViewDidChange(_ textView: UITextView) {
            stringDidChange?(textView.text)
        }
    }
}
MyUITextView(text: $uitextView) {
    // this HStack is for imitating the UIToolbar
    HStack {
        Button("Click") { print("Foo") }
            .padding()
        Spacer()
    }
    .background(.bar.shadow(.drop(radius: 1)))
}
.border(.black, width: 1)
.frame(maxHeight: 40)

Ios相关问答推荐

第三方框架的iOS隐私声明

有没有办法观察滚动的SwiftUI图表的内容偏移量?

RxSwift:如何使用另外两个可观测对象(其中一个依赖于另一个)创建可观测对象?

如何在SwiftUI中顺时针和逆时针两个方向应用旋转效果?

当文本为空时,具有垂直轴的SwiftUI文本字段不会与FirstTextBaseline对齐

Flutter iOS 键盘问题:. TextInputType.number 中缺少字符

在标签中显示多个计数器但在同一屏幕(Swift,IOS)

SwiftUI 我自己的渲染函数的返回类型是什么?

如何在不支持并发的自动关闭中修复'async'调用?

在向我的 struct 添加属性后获取线程 10:EXC_BAD_ACCESS (code=EXC_I386_GPFLT)

如何在 Swift 中旋转 UIButton 和 UILabel 的文本?

如何使用情节提要重新排列 UITabBarController 项目?

CocoaPods 找不到 podFirebase/Core的兼容版本 | cloud_firestore, Flutter

在 Xcode 5 中为超级视图添加间距约束

从 NSUserDefaults 字典 iOS 中删除所有键

将自定义对象保存到 NSUserDefaults

UITextView 从文本的底部或中间开始

iOS:以编程方式制作屏幕截图的最快、最高效的方法是什么?

swift 语言中的 null / nil

iTunes Connect 中的无限制 Web 访问是什么意思