简体   繁体   English

SwiftUI 文本编辑器滚动显示键盘不起作用

[英]SwiftUI TextEditor Scroll with Show Keyboard Not Working

I have a SwiftUI application with SwiftUI lifecycle that uses TextFields and TextEditors.我有一个带有 SwiftUI 生命周期的 SwiftUI 应用程序,它使用 TextFields 和 TextEditors。 I have written a modifier to deal with scrolling the content that would be underneath the keyboard when the keyboard is present.我编写了一个修饰符来处理在键盘存在时滚动键盘下方的内容。 My modifier works as expected for TextFields but not at all for TextEditors.我的修饰符对于 TextFields 可以正常工作,但对于 TextEditors 则完全不同。 The Apple docs say the keyboardWillShowNotification is posted immediately prior to the display of the keyboard. Apple 文档说,keyboardWillShowNotification 是在键盘显示之前立即发布的。 I cannot find anything which states the notification is limited to only TextFields.我找不到任何声明通知仅限于 TextFields 的内容。 Both field types are in the same ScrollView.两种字段类型都在同一个 ScrollView 中。 Hopefully I'm missing something simple here.希望我在这里遗漏了一些简单的东西。

My modifier:我的修改器:

struct AdaptsToSoftwareKeyboard: ViewModifier {

    @State var currentHeight: CGFloat = 0

    func body(content: Content) -> some View {
        content
            .padding(.bottom, self.currentHeight)
            .edgesIgnoringSafeArea(self.currentHeight == 0 ? Edge.Set() : .bottom)
            .onAppear(perform: subscribeToKeyboardEvents)
    }

    private let keyboardWillOpen = NotificationCenter.default
        .publisher(for: UIResponder.keyboardWillShowNotification)
        .map { $0.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect }
        .map { $0.height }

    private let keyboardWillHide =  NotificationCenter.default
        .publisher(for: UIResponder.keyboardWillHideNotification)
        .map { _ in CGFloat.zero }

    private func subscribeToKeyboardEvents() {
        _ = Publishers.Merge(keyboardWillOpen, keyboardWillHide)
            .subscribe(on: RunLoop.main)
            .assign(to: \.self.currentHeight, on: self)
    }
}

Any guidance would be appreciated.任何指导将不胜感激。 Xcode Version 12.2 beta (12B5018i) iOS 14 Xcode 12.2 测试版 (12B5018i) iOS 14

First edit: I thought the issue was likely a failure to receive notifications when the TextEditor receives the focus.第一次编辑:我认为问题可能是在 TextEditor 获得焦点时无法收到通知。 However, adding this modifier to the TextEditor shows that the notification is fired.但是,将此修饰符添加到 TextEditor 显示通知已触发。 No progress:没有进展:

   .onReceive(NotificationCenter.default.publisher(for: UIApplication.keyboardWillShowNotification)) { _ in
        print("Cursor is in TextEditor!")
    }

I assume you should store subscriber as below我假设你应该存储订阅者如下

struct AdaptsToSoftwareKeyboard: ViewModifier {

    @State var currentHeight: CGFloat = 0
    @State private var cancelable: AnyCancellable?
    
    func body(content: Content) -> some View {
        content
            .padding(.bottom, self.currentHeight)
            .edgesIgnoringSafeArea(self.currentHeight == 0 ? Edge.Set() : .bottom)
            .onAppear(perform: subscribeToKeyboardEvents)
    }

    private let keyboardWillOpen = NotificationCenter.default
        .publisher(for: UIResponder.keyboardWillShowNotification)
        .map { $0.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect }
        .map { $0.height }

    private let keyboardWillHide =  NotificationCenter.default
        .publisher(for: UIResponder.keyboardWillHideNotification)
        .map { _ in CGFloat.zero }

    private func subscribeToKeyboardEvents() {
        self.cancelable = Publishers.Merge(keyboardWillOpen, keyboardWillHide)
            .subscribe(on: RunLoop.main)
            .assign(to: \.self.currentHeight, on: self)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM