简体   繁体   English

更新 SwiftUI 中的 inputAccessoryView (UIViewRepresentable)

[英]Update inputAccessoryView in SwiftUI (UIViewRepresentable)

I want to display search results above the keyboard with SwiftUI.我想用 SwiftUI 在键盘上方显示搜索结果。 For this, I made a UIViewRepresentable for UITextField and set the inputAccessoryView accordingly.为此,我为UITextField制作了一个UIViewRepresentable并相应地设置了inputAccessoryView

Everything is displayed correctly, but the inputAccessoryView is not updating.一切都正确显示,但inputAccessoryView没有更新。

 struct V_ToolbarTextField<Content: View>: UIViewRepresentable {
    
    var title: String
    @Binding var text: String
    var keyType: UIKeyboardType
    
    var toolbarContent: Content
        
    init(_ title: String,
         text: Binding<String>,
         keyType: UIKeyboardType = .alphabet,
         toolbarContent: Content) {
        self.title = title
        self._text = text
        self.keyType = keyType
        self.toolbarContent = toolbarContent
    }
    
    
    func makeUIView(context: Context) -> UITextField {
        let textfield = UITextField()
        textfield.keyboardType = keyType
        textfield.placeholder = title

        let textUIKit = UIHostingController(rootView: toolbarContent)
        textUIKit.view.frame = CGRect(x: 0, y: 0, width: 800, height: 60)

        textfield.inputAccessoryView = textUIKit.view
        return textfield
    }
    
    func updateUIView(_ uiView: UITextField, context: Context) {
        uiView.text = text
    }
}

This is how I am passing the inputAccessoryView 's content:这就是我传递inputAccessoryView内容的方式:

 V_ToolbarTextField("Title",
                    text: $gameNameFilter, 
                    toolbarContent: 
                     viewContainingSearchResults())

I've tried binding toolbarContent and setting the inputAccessoryView in updateUIView() .我已经尝试绑定toolbarContent并在updateUIView()中设置inputAccessoryView I've also tried creating a Coordinator class and setting the UITextField 's delegate to it, hoping to use a UITextFieldDelegate function to update it, but I don't know which one and I wasn't able to set the delegate correctly.我还尝试创建一个 Coordinator 类并将UITextField的委托设置为它,希望使用UITextFieldDelegate函数来更新它,但我不知道是哪一个并且我无法正确设置委托。

I couldn't get it to work and ditched the inputAccessoryView for a VStack with the ToolBar at the bottom.我无法让它工作,并放弃了inputAccessoryView以获得底部带有 ToolBar 的VStack I hide it when the keyboard is shown.显示键盘时我将其隐藏。

@State var keyboardOpen = false

VStack {
    
    // everything else...
    
    if (keyboardOpen) {
        V_FilteredGameList(filter: vm.gameName, action: { game in
            vm.setExistingGame(game: game)
            hideKeyboard()
        })
    }
}
.onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification)) { _ in
        keyboardOpen = true
}
.onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification)) { _ in
        keyboardOpen = false
}

Try calling reloadInputViews() on your textfield.尝试在您的文本字段上调用 ​​reloadInputViews()。 Most likely you want to do it on updateUIView function.很可能您想在 updateUIView 函数上执行此操作。

open func reloadInputViews() by Apple documentation: // If called while object is first responder, reloads inputView, inputAccessoryView, and textInputMode. Apple 文档中的 open func reloadInputViews(): // 如果在对象是第一响应者时调用,则重新加载 inputView、inputAccessoryView 和 textInputMode。 Otherwise ignored否则忽略

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

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