【发布时间】: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 中的按钮执行此操作。我对代码进行了编辑。假设我在那里有一个按钮,你将如何从它的操作中做到这一点