【问题标题】:Integrating UIViewController with Storyboard into SwiftUI Project: A Gap at the Top after Transition将 UIViewController 与 Storyboard 集成到 SwiftUI 项目中:Transition 后的顶部差距
【发布时间】: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


【解决方案1】:

我假设它是NavigationView 的空大标题区域,所以只需隐藏它

    NavigationView {
        VStack {
            NavigationLink(destination: HomeViewControllerRepresentation()) {
                Text("Tap me")
            }
        }
        .navigationBarTitle("")
        .navigationBarHidden(true)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 2021-12-28
    相关资源
    最近更新 更多