0

I am embedding SwiftUI View inside a HostingViewController and added the hosting viewcontroller view on ViewController subview. Initially the size of SwiftUI view is calculated by intrinsic content size of Text in it. But when the Text inside the SwiftUI view is updated, let say some size reduce, i want to update the size of HostingViewController view as well. Example:

This is my SwiftUI View and its size is based on the Text size in the body. Let say it renders currently 5 Text item and after 5 sec i am updating the item in the view model to 2 and which updates the SwiftUI view body and updated the text in it so the size would decrease of this SwiftUI view but its HostingViewController still having the view of the earlier size. How i can update the HostingViewController view size when the body size is updated?

class TextWidgetViewModel: ObservableObject {
    @Published var texts: [TextModel]
    
    public init(_ texts: [TextModel]) {
        self.texts = texts
   
        DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
            print("dispatch queue called")
            let texts1: [TextModel] = [TextModel(text: "Updated Text 1"),
                                         TextModel(text: "Updated Text 2")]
            
            
            self.texts = texts1
        }
    }
}
struct TextWidgetView: View {
    
    @ObservedObject var viewModel: TextWidgetViewModel
 
    init(_ viewModel: TextWidgetViewModel) {
        self.viewModel = viewModel
    }
    
    var body: some View {
        ForEach(0..<viewModel.texts.count, id: \.self) { index in
            let textModel = viewModel.texts[index]
            Text(textModel.text ?? "")
                .background(Color.green)
        }
     }
}



 let texts: [TextModel] = [TextModel(text: "Hello"),
                                 TextModel(text: "Let's get started"),
                                 TextModel(text: "Let Rock and Roll"),
                                 TextModel(text: "Text 1"),
                                 TextModel(text: "Text 2"),
                                 TextModel(text: "Text 3"),
                                 TextModel(text: "Text 4"),
                                 TextModel(text: "Text 5"),
                                 TextModel(text: "Text 6"),
                                 TextModel(text: "Text 7"),
                                 TextModel(text: "Text 8"),
                                 TextModel(text: "Text 9")]
        
        let view = TextWidgetView(TextWidgetViewModel(texts))
        let vc = UIHostingController(rootView: view)
        let someView = vc.view
        self.view = someView
        addInUIViewContainer()

enter image description here

Here white portion of the image is UIHostingViewController view and initially the size of this is based on the number of Text item in SwiftUI View but once the Text is updated, body of SwiftUI View rerenders and its size updated. But the size of HostinViewController View remain same. Thanks, please help in thisenter image description here

MUKUL BAKSHI
  • 11
  • 1
  • 1

0 Answers0