微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

添加到 UITextView 的工具栏附件作为 UIViewRepresentable 仅在首次启动 iMessage 扩展应用程序后显示

如何解决添加到 UITextView 的工具栏附件作为 UIViewRepresentable 仅在首次启动 iMessage 扩展应用程序后显示

希望工具栏一直显示

非常感谢任何帮助,因为这对用户体验来说是一个真正的拖累。

我为 TextView 的键盘添加一个工具栏,如下所示。
但是,工具栏仅在应用程序运行一次后显示。这意味着工具栏不会在应用程序第一次运行时显示。该应用程序每次在初始加载后都能正常工作。

适用于 IOS 14.3、Xcode 12.3、Swift 5、iMessage 扩展应用程序。在模拟器或真机上失败。

struct CustomTextEditor: UIViewRepresentable {
    @Binding var text: String
    private var returnType: UIReturnKeyType
    private var keyType: UIKeyboardType
    private var displayDoneBar: Bool
    private var commitHandler: (()->Void)?
    
    init(text: Binding<String>,returnType: UIReturnKeyType = .done,keyboardType: UIKeyboardType,displayDoneBar: Bool,onCommit: (()->Void)?) {
        self._text = text
        self.returnType = returnType
        self.keyType = keyboardType
        self.displayDoneBar = displayDoneBar
        self.commitHandler = onCommit
    }
    
    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
        
        textView.keyboardType = keyType
        textView.returnKeyType = returnType
        textView.backgroundColor = .clear
        textView.font = UIFont.systemFont(ofSize: 20,weight: .regular)
        textView.isEditable = true
        textView.delegate = context.coordinator
        if self.displayDoneBar {
            let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace,target: self,action: nil)
            let doneButton = UIBarButtonItem(title: "Close Keyboard",style: .done,action: #selector(textView.doneButtonpressed(button:)))
            
           let toolBar = UIToolbar(frame: CGRect(x: 0,y: 0,width: 300,height: 50))
            toolBar.items = [flexibleSpace,doneButton,flexibleSpace]
            toolBar.setItems([flexibleSpace,flexibleSpace],animated: true)
            toolBar.sizetoFit()
            textView.autoresizingMask = [.flexibleHeight,.flexibleWidth]
            textView.translatesAutoresizingMaskIntoConstraints = true
            textView.inputAccessoryView = toolBar

        }

        return textView
    }

    func updateUIView(_ textView: UITextView,context: Context) {
        textView.text = text
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    class Coordinator: NSObject,UITextViewDelegate {

        var parent: CustomTextEditor

        init(_ textView: CustomTextEditor) {
            self.parent = textView
        }

        func textViewDidChange(_ textView: UITextView) {
            self.parent.$text.wrappedValue = textView.text
        }
        func textViewDidEndEditing(_ textView: UITextView) {
            self.parent.$text.wrappedValue = textView.text
            parent.commitHandler?()
        }
    }
    
}

extension UITextView {
    @objc func doneButtonpressed(button:UIBarButtonItem) -> Void {
       self.resignFirstResponder()
    }
}

这就是它的名字...

import SwiftUI
    
final class ContentViewHostController: UIHostingController<ContentView> {
        weak var myWindow: UIWindow?
        init() {
            super.init(rootView: ContentView())
        }
        required init?(coder: NSCoder) {
            super.init(coder: coder,rootView: ContentView())
        }
    }
let kTextColor = Color(hex: "3E484F")
let kOverlayRadius: CGFloat = 10
let kOverlayWidth: CGFloat = 2
let kOverlayColor = kTextColor
        
struct ContentView: View {
    @State var text = ""
            
    var body: some View {
        vstack {
            Spacer()
            CustomTextEditor(text: $text,returnType: .default,keyboardType: .default,displayDoneBar: true,onCommit: nil)
                        .foregroundColor(kTextColor)
                        .overlay(
                            RoundedRectangle(cornerRadius: kOverlayRadius)
                                .stroke(kOverlayColor,linewidth: kOverlayWidth)
                        )
                        .frame(width: 200,height: 100,alignment: .center)
                    Spacer()
                }
            }
        }
        
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

From MessagesViewController...

override func willBecomeActive(with conversation: MSConversation) {
    let childViewCtrl = ContentViewHostController()
    childViewCtrl.view.layoutIfNeeded() // avoids snapshot warning?
        
    if let window = self.view.window {
        childViewCtrl.myWindow = window
        window.rootViewController = childViewCtrl
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。