【发布时间】:2020-10-08 00:31:59
【问题描述】:
我有一个示例 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")
}
}
}
}
}
如果我点击链接,我将被转发到ViewVontroller。这很好,只是我在顶部有一个很大的差距。它来自哪里,我该如何摆脱它?谢谢。
【问题讨论】:
-
看起来你可能有两个导航。一种是 Storyboard,另一种是 SwiftUI,这会导致类似的情况。
-
@MwcsMac 我的故事板不包含导航控制器。而且我的视图控制器没有连接到导航控制器。
-
我曾经遇到过类似的问题,这是因为我有一个子
ViewController父视图被限制在父视图顶部而不是安全区域。
标签: ios swift uiviewcontroller storyboard swiftui