将 UIViewController 与 Storyboard 集成到 SwiftUI 项目中:过渡后顶部的差距
Integrating UIViewController with Storyboard into SwiftUI Project: A Gap at the Top after Transition
我有一个示例 SwiftUI 项目来了解我是否可以从 SwiftUI View
过渡到 UIKit UIViewController
。我可以。
以下是我的UIViewController
.
import UIKit
import SwiftUI
class HomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.orange
}
}
struct HomeViewControllerRepresentation: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<HomeViewControllerRepresentation>) -> HomeViewController {
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "homeViewController") as! HomeViewController
}
func updateUIViewController(_ uiViewController: HomeViewController, context: UIViewControllerRepresentableContext<HomeViewControllerRepresentation>) {
}
}
我有 SwiftUI View
如下。
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: HomeViewControllerRepresentation()) {
Text("Tap me")
}
}
}
}
}
如果我点击 link,我将被转发到 ViewVontroller
。这很好,除了我在顶部有一个很大的差距。它来自哪里,我该如何摆脱它?谢谢。
我假设它是 NavigationView
的大 header 区域,所以只是隐藏它
NavigationView {
VStack {
NavigationLink(destination: HomeViewControllerRepresentation()) {
Text("Tap me")
}
}
.navigationBarTitle("")
.navigationBarHidden(true)
}
我有一个示例 SwiftUI 项目来了解我是否可以从 SwiftUI View
过渡到 UIKit UIViewController
。我可以。
以下是我的UIViewController
.
import UIKit
import SwiftUI
class HomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.orange
}
}
struct HomeViewControllerRepresentation: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<HomeViewControllerRepresentation>) -> HomeViewController {
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "homeViewController") as! HomeViewController
}
func updateUIViewController(_ uiViewController: HomeViewController, context: UIViewControllerRepresentableContext<HomeViewControllerRepresentation>) {
}
}
我有 SwiftUI View
如下。
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: HomeViewControllerRepresentation()) {
Text("Tap me")
}
}
}
}
}
如果我点击 link,我将被转发到 ViewVontroller
。这很好,除了我在顶部有一个很大的差距。它来自哪里,我该如何摆脱它?谢谢。
我假设它是 NavigationView
的大 header 区域,所以只是隐藏它
NavigationView {
VStack {
NavigationLink(destination: HomeViewControllerRepresentation()) {
Text("Tap me")
}
}
.navigationBarTitle("")
.navigationBarHidden(true)
}