UIKit: How can i resize a Viewcontroller to fit splitview primary column?

541 Views Asked by At

In my UIKit App i am using a UISplitViewController() as my rootViewController. I configure it in my SceneDelegate like this:

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
}

As you can see, i am using the CalendarViewController (wrapped inside a UINavigationController) as my primaryViewController and for the secondary view i am using a SwiftUI View, wrapped in a UIHostingController

The result looks like this:

enter image description here

As you can see, the CalendarViewController fits the iPhone Screen but not the iPad Primary Column. The CalendarViewController comes from CalendarKit, a Swift Calendar Library.

What do i need to change in the CalendarViewController to fit the SplitView Primary Column? Any ideas?

Reference: CalendarViewController

1

There are 1 best solutions below

0
Niklas On

for all those who also face the problem: after some try and error i found a solution to fix the layout issue. In my case the key was to change views layout from frame to autolayout like this:

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

you can find more information here:

iOS 14 UISplitViewController: 5 Issues That You May Run Into