'How do I trigger updateUIView of a UIViewRepresentable?
I'm trying to implement the uiView from SpritzSwift into a SwiftUI app, but after the first rendering I can't get it to update. The manager that drives the UIView is working, but the UIView itself is not updating. I expected either that the UiView.setNeedsDisplay() inside the view controller or the changes in the @Bindable variables inside the wrapper would trigger updateUIView, but no dice.
No matter what change I make to the UiView or to its wrapper, it never gets updated (for example, the background never gets updated to clear color in the code example). How do I get this view to update?
Here's the SwiftUI Code:
import SwiftUI
struct Content: View {
@State public var ssManager:SpritzSwiftManager = SpritzSwiftManager(withText: "", andWordPerMinute: Int(200))
@State public var ssView:SpritzSwiftView = SpritzSwiftView(frame: CGRect(x: 0, y: 0, width: 200, height: 600 ))
@State private var currentWord = ""
var body: some View {
VStack {
Text("SpritzTest")
.padding()
let spritzUIView = SpritzUIViewRepresentable(SpritzView: $ssView,SpritzViewManager:$ssManager, CurrentWord: $currentWord)
spritzUIView.padding()
Button(action:
{
ssManager = SpritzSwiftManager(withText: "Text try one two three", andWordPerMinute: 200)
spritzUIView.SpritzView = SpritzSwiftView(frame: CGRect(x: 0, y: 0, width: 200, height: 40 ))
spritzUIView.SpritzView.backgroundColor = .clear
ssManager.startReading { (word, finished) in
if !finished {
self.ssView.updateWord(word!)
currentWord = word!.word
spritzUIView.CurrentWord = currentWord
}
}
})
{
Text("Start")
}
}
}
}
and here's the wrapper:
struct SpritzUIViewRepresentable : UIViewRepresentable{
@Binding var SpritzView:SpritzSwiftView
@Binding var SpritzViewManager:SpritzSwiftManager
@Binding var CurrentWord:String
func makeUIView(context: Context) -> SpritzSwiftView {
return SpritzView
}
func updateUIView(_ uiView: SpritzSwiftView, context: Context) {
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
