Wednesday, November 11, 2026
HomeiOS Developmentios - Accessing Worth of SwiftUI View by way of a UIKit...

ios – Accessing Worth of SwiftUI View by way of a UIKit View

[ad_1]

I learn this query on-line and located it to be very fascinating. You probably have a SwiftUI view as proven under. How will you entry the chosen score in a UIKit view with out altering a single line in RatingView.

struct RatingView: View {
    
    @Binding var score: Int?
    
    non-public func starType(index: Int) -> String {
        
        if let score = score {
            return index <= score ? "star.fill" : "star"
        } else {
            return "star"
        }
    }
    
    var physique: some View {
        HStack {
            ForEach(1...5, id: .self) { index in
                Picture(systemName: self.starType(index: index))
                    .foregroundColor(Coloration.orange)
                    .onTapGesture {
                        score = index
                }
            }
        }
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        self.view.backgroundColor = .white
        
        // .fixed(3) will likely be changed by one thing within the ViewController so it will probably deal with the modifications for the score
        let hostingController = UIHostingController(rootView: RatingView(score: .fixed(3)))
        
        guard let ratingsView = hostingController.view else { return }
        self.addChild(hostingController)
        
        self.view.addSubview(ratingsView)
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments