Stack 中的 SwiftUI TextView 不起作用

问题描述 投票:0回答:0

我无法在 VStack 中添加 TextView 组件,并且它不想打开键盘以便用户可以键入。它确实通过 onTapGesture 注册了一个点击,所以我不知道为什么一个基本的东西不起作用。

我在几何阅读器中试过,但没有用。我试过对着 Mac 大喊大叫,但没用。最后的希望是 StackOverflow。

这是 textView 包装器代码:

struct TextView: UIViewRepresentable {

@Binding var text: String
var placeholder: String

func makeUIView(context: Context) -> UITextView {
    let textView = UITextView()
    textView.isEditable = true
    textView.isSelectable = true
    textView.delegate = context.coordinator
    textView.font = UIFont.systemFont(ofSize: UIFont.labelFontSize)
    textView.text = text.isEmpty ? placeholder : text
    textView.textColor = text.isEmpty ? UIColor.red : UIColor.black
    return textView
}

func updateUIView(_ uiView: UITextView, context: Context) {
    uiView.text = text
    uiView.textColor = text.isEmpty ? UIColor.red : UIColor.black
}

func makeCoordinator() -> Coordinator {
    Coordinator(text: $text)
}

class Coordinator: NSObject, UITextViewDelegate {
    @Binding var text: String
    
    init(text: Binding<String>) {
        _text = text
    }
    
    func textViewDidChange(_ textView: UITextView) {
        text = textView.text
    }
}

}

这是里面有 Stack 的损坏代码:

var body: some View {
    GeometryReader { geo in
        VStack(alignment: .leading, spacing: 16) {
            Text(vm.category?.question ?? "")
                .foregroundColor(Color(hex: config.theme.textColor))
                .font(.system(size: 40))
                .fontWeight(.black)
                .fixedSize(horizontal: false, vertical: true)
            Text(vm.category?.description ?? "")
                .foregroundColor(Color(hex: config.theme.textColor).opacity(0.5))
                .font(.body)
                .fontWeight(.medium)
                .fixedSize(horizontal: false, vertical: true)
                .padding(.bottom, 10)
            VStack(alignment: .leading) {
                Image(vm.category?.icon ?? "Question")
                    .resizable()
                    .renderingMode(.template)
                    .foregroundColor(Color(hex: config.theme.primaryColor))
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 24, height: 24)
                TextView(text: $vm.text, placeholder: "Tap to type")
                    .frame(maxWidth: .infinity, minHeight: 230 + (config.showLog ? 0 : 120), alignment: .top)
            }
            .padding()
            .addBorder(Color(hex: config.theme.textColor).opacity(0.15), width: 1, cornerRadius: CGFloat(config.theme.cornerRadius))
            .frame(maxWidth: .infinity, minHeight: 280, alignment: .top)
            
            Button {
                dismiss()
            } label: {
                Text("Send")
            }
        }
        .padding(.horizontal)
        .frame(maxWidth: .infinity, minHeight: geo.frame(in: .global).height, alignment: .top)
    }
}
swiftui uitextview
© www.soinside.com 2019 - 2024. All rights reserved.