UITEXTVIEW INPUTACCESSORYVIEW在安全区域手机上行为不正确?

发布于 2025-02-08 12:48:44 字数 2422 浏览 1 评论 0原文

我正在尝试在SwiftUi项目中使用UiviewRepresentable在SwiftUI项目中使用Uikit的UitextView。一切都很好,但是我面临着具有安全区域的手机的InputacCessoryView的问题。 我的InputAccessoryView是用UihostingController包裹的SwiftUI视图,使我可以将SwiftUI视图用作UIVIEW。 我已经附加了代码和一些图像,为了简化我的问题,我使用的是简单的SwiftUi ColorView作为InputAccessoryView。如您所见,当SwiftUi视图靠近底部安全区域时,它会从InputacCessoryView的框架中移出。当您向下滑动以关闭键盘时,它会奇怪地消失。我已经尝试了一切,但找不到解决方案。你能帮我吗?

谢谢你!

uiviewRepresentable,uitextview

struct UITextViewWrapper: UIViewRepresentable {
    @Binding var text: String
    
    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
        textView.text = text
        textView.inputAccessoryView = makeKeyboardBar()
        textView.keyboardDismissMode = .interactive
        textView.alwaysBounceVertical = true
        
        textView.delegate = context.coordinator
        textView.becomeFirstResponder()
        
        return textView
    }
    
    func updateUIView(_ uiView: UITextView, context: Context) {
        //
    }
    
    class Coordinator: NSObject, UITextViewDelegate {
        let parent: UITextViewWrapper
        
        init(_ parent: UITextViewWrapper) {
            self.parent = parent
        }
        
        func textViewDidChange(_ textView: UITextView) {
            parent.text = textView.text
        }
    }
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }
    
    func makeKeyboardBar() -> UIView {
        let swiftUIView = ColorView()
        let hostingController = UIHostingController(rootView: swiftUIView)
        
        let uiView = hostingController.view!
        uiView.frame = CGRect(origin: .zero, size: hostingController.sizeThatFits(in: uiView.frame.size))
        
        uiView.layer.borderWidth = 1
        uiView.layer.borderColor = UIColor.red.cgColor
        
        return uiView
    }
}

swiftui视图用于用作InputAccessoryView

struct ColorView: View {
    
    var body: some View {
        Color.yellow
        .frame(height: 55)
    }
}

contentView

struct ContentView: View {
    @State private var text = "Hello World"
    
    var body: some View {
        UITextViewWrapper(text: $text)
    }
}

映像

I am trying use UIKit's UITextView in an swiftUI project using UIViewRepresentable. Everything works great but I am facing some issue with inputAccessoryView on phones with safe area.
My inputAccessoryView is a swiftUI view wrapped in UIHostingController which lets me use swiftUI views as UIView.
I have attached my code and some images, To simplify my issue I am using simple swiftUI colorView as inputAccessoryView. As you can see the swiftUI view moves out of the frame of inputAccessoryView when it comes near bottom safe area. When you swipe down to close the keyboard it disappears weirdly. I have tried everything but could not find the solution. Can you please help me?

Thank You!

UIViewRepresentable, UITextView

struct UITextViewWrapper: UIViewRepresentable {
    @Binding var text: String
    
    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
        textView.text = text
        textView.inputAccessoryView = makeKeyboardBar()
        textView.keyboardDismissMode = .interactive
        textView.alwaysBounceVertical = true
        
        textView.delegate = context.coordinator
        textView.becomeFirstResponder()
        
        return textView
    }
    
    func updateUIView(_ uiView: UITextView, context: Context) {
        //
    }
    
    class Coordinator: NSObject, UITextViewDelegate {
        let parent: UITextViewWrapper
        
        init(_ parent: UITextViewWrapper) {
            self.parent = parent
        }
        
        func textViewDidChange(_ textView: UITextView) {
            parent.text = textView.text
        }
    }
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }
    
    func makeKeyboardBar() -> UIView {
        let swiftUIView = ColorView()
        let hostingController = UIHostingController(rootView: swiftUIView)
        
        let uiView = hostingController.view!
        uiView.frame = CGRect(origin: .zero, size: hostingController.sizeThatFits(in: uiView.frame.size))
        
        uiView.layer.borderWidth = 1
        uiView.layer.borderColor = UIColor.red.cgColor
        
        return uiView
    }
}

SwiftUI View to use as inputAccessoryView

struct ColorView: View {
    
    var body: some View {
        Color.yellow
        .frame(height: 55)
    }
}

ContentView

struct ContentView: View {
    @State private var text = "Hello World"
    
    var body: some View {
        UITextViewWrapper(text: $text)
    }
}

image
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风为裳 2025-02-15 12:48:44

Swiftui具有自己的安全区域跟踪,因此您必须在Swiftui视图中明确管理安全区域行为,

var body: some View {
    Color.yellow.edgesIgnoringSafeArea(.bottom)  // << here !!
    .frame(height: 55)
}

SwiftUI has own safe area tracking so you have to manage safe area behaviour explicitly inside SwiftUI view, like

var body: some View {
    Color.yellow.edgesIgnoringSafeArea(.bottom)  // << here !!
    .frame(height: 55)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文