1

在我的 UIKit 应用程序中,我使用 aUISplitViewController()作为我的 rootViewController。我SceneDelegate像这样配置它:

var splitView: UISplitViewController?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    self.makeSplitViewController()
    
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = self.splitView
        self.window = window
        window.makeKeyAndVisible()

        self.splitView?.viewController(for: .secondary)?.navigationController?.navigationBar.barStyle = .black
    }
}

func makeSplitViewController() {
    let splitViewController = UISplitViewController(style: .doubleColumn)
    splitViewController.preferredDisplayMode = .oneBesideSecondary
    
    let primaryViewController = UINavigationController(rootViewController: CalendarViewController())
    let secondaryViewController = UIHostingController(rootView: EventDetailView())
    
    splitViewController.setViewController(primaryViewController, for: .primary)
    splitViewController.setViewController(secondaryViewController, for: .secondary)
    splitViewController.setViewController(primaryViewController, for: .compact)
    
    self.splitView = splitViewController
}

如您所见,我使用CalendarViewController(包装在 UINavigationController 中)作为我的 primaryViewController,而对于辅助视图,我使用的是 SwiftUI 视图,包装在UIHostingController

结果如下所示:

在此处输入图像描述

如您所见,CalendarViewController 适合 iPhone 屏幕,但不适合 iPad 主列。来自CalendarKitCalendarViewController,一个 Swift 日历库。

我需要在 CalendarViewController 中进行哪些更改以适应 SplitView 主列?有任何想法吗?

参考:CalendarViewController

4

1 回答 1

1

对于所有同样面临问题的人:经过一些尝试和错误,我找到了解决布局问题的解决方案。在我的情况下,关键是将视图布局从如下更改frame为:autolayout

dayHeaderView.leadingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leadingAnchor).isActive = true
dayHeaderView.trailingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.trailingAnchor).isActive = true
dayHeaderView.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor).isActive = true
dayHeaderView.heightAnchor.constraint(equalToConstant: headerHeight).isActive = true

您可以在这里找到更多信息:

iOS 14 UISplitViewController:你可能会遇到的 5 个问题

于 2021-10-05T16:19:32.093 回答