【问题标题】:how to navigate from swiftui to viewcontroller using existing navigation controller如何使用现有的导航控制器从 swiftui 导航到 viewcontroller
【发布时间】:2021-03-17 12:26:23
【问题描述】:

我有一个导航控制器,它导航到一个视图控制器,它导航到 uihostingcontroller。我将如何从 swift ui 推送到另一个视图控制器

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        self.window = UIWindow(frame: windowScene.coordinateSpace.bounds)
        window?.windowScene = windowScene
        let navigationView = UINavigationController(rootViewController: PreviewViewVideoController())
        navigationView.isToolbarHidden = true
        self.window?.rootViewController = navigationView
        self.window?.makeKeyAndVisible()
    }

在预览视频控制器中

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch cards[indexPath.row] {
        case .trans_human:
            let controller = UIHostingControllerCustom(rootView: FilmOverviewView(overview: TransHumanOverview()))
            self.navigationController?.pushViewController(controller, animated: true)
            controller.navigationItem.title = cards[indexPath.row].rawValue
}
}

在影片概览视图中

struct FilmOverviewView: View {
    var filmOverview: FilmOverview!
    var imageResource: String!
    
    init(overview: FilmOverview) {
        filmOverview = overview
        
    }

var body: some View {
    ScrollView(Axis.Set.vertical, showsIndicators: false) {
        VStack {
            Image(filmOverview.imageResource)
                .resizable()
                .frame(width: UIScreen.main.bounds.width,
                       height: UIScreen.main.bounds.height / 2)
                .overlay(StartButtonOverlay(), alignment: .bottom)
            Button(action: {})

                
}
}

如何使用现有的导航堆栈从 swiftui 按钮视图操作导航到新的视图控制器

【问题讨论】:

  • SwiftUI 无法与 UINavigationController 一起使用,因此您需要继续使用 UIKit 在这种情况下进行导航。
  • 我将如何通过 swiftui 中的按钮执行此操作。我对代码进行了编辑。假设我在那里有一个按钮,你将如何从它的操作中做到这一点

标签: ios swift swiftui uikit


【解决方案1】:

我会通过环境值将UINavigationController 注入 SwiftUI 堆栈

struct NavigationControllerKey: EnvironmentKey {
    static let defaultValue: UINavigationController? = nil
}

extension EnvironmentValues {
    var navigationController: NavigationControllerKey.Value {
        get {
            return self[NavigationControllerKey.self]
        }
        set {
            self[NavigationControllerKey.self] = newValue
        }
    }
}

然后在表视图委托中注入它

let controller = UIHostingControllerCustom(rootView: 
      FilmOverviewView(overview: TransHumanOverview()
         .environment(\.navigationController, self.navigationController))

现在在FilmOverviewView 我们可以使用它

struct FilmOverviewView: View {
   @Environment(\.navigationController) var navigationController
   // ... other code

   var body: some View {
       ScrollView(Axis.Set.vertical, showsIndicators: false) {
          VStack {
            // ... other code

            Button("Demo") {
               let controller = UIHostingControllerCustom(rootView: 
                  SomeOtherView()   // inject if needed again
                     .environment(\.navigationController, self.navigationController)))
               self.navigationController?.pushViewController(controller, animated: true)
            }
   }
}

更新:更新了自定义主机控制器

class UIHostingControllerCustom<V: View>: UIHostingController<V> {
    override func viewWillAppear(_ animated: Bool) { 
        navigationController?.setNavigationBarHidden(true, animated: false)
    }
}

【讨论】:

  • let controller = UIHostingControllerCustom(rootView: FilmOverviewView(overview: TransHumanOverview()).environment(\.navigationController, self.navigationController)) 在表视图中抛出无法将“某些视图”类型的值转换为预期参数类型“FilmOverviewView”
  • 您需要更正自定义 UIHostingControllerCustom 类中的泛型。
  • class UIHostingControllerCustom:UIHostingController{ override func viewWillAppear(_ animated: Bool) { navigationController?.setNavigationBarHidden(true, animated: false) } } 我需要在这里改变什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 2021-05-11
  • 1970-01-01
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多