Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

swift - Going to another View from SwiftUI to UIKit?

I use uihostingcontroller to load a SwiftUI view in a UIKit View.

in my SwiftUI view, I create some horizontal ScrollViews with some stuff in them.

I need to be able to click/tap on these elements and go to another view in my UIKit.

Is this possible?

I found this but this shows to "reload" the UIKit into the SwiftUI view which is not what I want to do and I don't think this is the correct way of doing this anyway:

Is there any way to change view from swiftUI to UIKit?

This is my SwiftUI code:

import SwiftUI

struct videosContentView: View {
    var body: some View {
        ScrollView{
            ForEach(0..<2) {_ in
         
        Section(header: Text("Important tasks")) {
                VStack{
                    ScrollView(.horizontal){
                        HStack(spacing: 20) {
                            ForEach(0..<10) {
                                
            
                                Text("Item ($0)")
                                    .font(.headline)
                                    .frame(width: 160, height: 200)
                                    .background(Color.gray)
                                    /*.padding()*/
                                    .addBorder(Color.white, width: 1, cornerRadius: 10)
                                    /*.overlay(
                                        RoundedRectangle(cornerRadius: 16)
                                            .stroke(Color.blue, lineWidth: 4)
                                    )*/
                               
                            }
                        }
                    }
                }
                    }
            
            }
        }
    }
}

struct videosContentView_Previews: PreviewProvider {
    static var previews: some View {
        videosContentView()
    }
}

extension View {
    public func addBorder<S>(_ content: S, width: CGFloat = 1, cornerRadius: CGFloat) -> some View where S : ShapeStyle {
        let roundedRect = RoundedRectangle(cornerRadius: cornerRadius)
        return clipShape(roundedRect)
             .overlay(roundedRect.strokeBorder(content, lineWidth: width))
    }
}

EDIT:

Based on suggestion in the comments, I tried this but this doesn't work:

Button(action: {

                            let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("home") as home
                            self.navigationController.pushViewController(secondViewController, animated: true)
                            
                            
                        }) {
                            Text("Dismiss me")
                                .font(.headline)
                                .frame(width: 160, height: 200)
                                .background(Color.gray)
                                .addBorder(Color.white, width: 1, cornerRadius: 10)
                        }
question from:https://stackoverflow.com/questions/65945162/going-to-another-view-from-swiftui-to-uikit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
struct YourSwiftUIView: View {
   @State var push = false
    var body: some View {
        if push {
            YourUIViewController()
        }else {
            //your content
            Button(action: {
                withAnimation() {
                    push.toggle()
                }
            }) {
                Text("Dismiss me")
                    .font(.headline)
                    .frame(width: 160, height: 200)
                    .background(Color.gray)
            }
        }
    }
}
struct YourUIViewController: UIViewControllerRepresentable {
    typealias UIViewControllerType = UIViewController
    func makeUIViewController(context: UIViewControllerRepresentableContext<YourUIViewController>) -> UIViewController {
        let yourUIViewController = UIViewController() //your UIViewController
        return yourUIViewController
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<YourUIViewController>) {
    }
}

this will change from the swiftuiview to the UIViewController.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...