【问题标题】:Going to another View from SwiftUI to UIKit?从 SwiftUI 转到 UIKit 的另一个视图?
【发布时间】:2021-05-02 19:45:34
【问题描述】:

我使用uihostingcontroller 在 UIKit 视图中加载 SwiftUI 视图。

在我的 SwiftUI 视图中,我创建了一些水平滚动视图,其中包含一些内容。

我需要能够点击/点按这些元素并转到我的 UIKit 中的另一个视图。

这可能吗?

我发现了这个,但这表明将 UIKit “重新加载”到 SwiftUI 视图中,这不是我想要做的,而且我认为这不是正确的做法:

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

这是我的 SwiftUI 代码:

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))
    }
}

编辑:

根据 cmets 中的建议,我尝试了这个,但这不起作用:

按钮(动作:{

                            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)
                        }

【问题讨论】:

  • 你的另一个视图是 UIViewController 还是别的什么?
  • @RajaKishan,是的,它是另一个 UIViewController。
  • 然后首先找到最顶层的控制器并推送你的视图控制器或者你可以使用完成块。
  • @RajaKishan,请查看我的编辑。如何从 SwiftUI 视图推送到另一个视图控制器?
  • 添加断点并检查你的 self.navigationController 是否为空,如果为空,则首先将导航控制器嵌入到主机控制器。

标签: swift swiftui uikit


【解决方案1】:
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>) {
    }
}

这将从 swiftuiview 变为 UIViewController。

【讨论】:

  • 我收到此错误:Cannot find 'rootview' in scope
  • 根视图是你的 UIVIewController
  • 好的,所以当我导航到另一个 UIVIewController 时,我的标签栏和导航栏都松开了。
  • 那么您应该将您的 UIViewController 包装在 UIViewControllerRepresentable 中并通过 .fullScreenCover 推送它。我会更新我的答案
  • 对不起,当我运行这段代码时,我得到了一个盲目的动画并且视图没有改变!
猜你喜欢
  • 1970-01-01
  • 2021-12-15
  • 2021-02-16
  • 2020-06-21
  • 1970-01-01
  • 2020-06-03
  • 2021-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多