1

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

Niklas
  • 1,638
  • 4
  • 19
  • 48
  • 1
    I'm guessing this is due to size classes. On an iPad, the default (read: in full screen) the root VC is *always* going to be "Regular", where on an iPhone it will likely be "Compact" in portrait mode. My first idea is to test my hypothesis out - have you tried using your iPad in split screen? That should clear up where things are not what you want. Next, I'd recommend (attribute to @matt) https://www.biteinteractive.com/split-view-controllers-done-right-in-ios-14/ where he showed me how *best* to make my split VC *not* be the root. It's gives you more flexibility, period. –  Oct 03 '21 at 14:00
  • 1
    @dfd thanks for your answer. In Splitscreen, it looks like this: https://i.stack.imgur.com/SwTiF.png for reference: https://github.com/richardtop/CalendarKit/issues/320#issuecomment-932959922 – Niklas Oct 03 '21 at 15:05
  • I probably needed to be more specific, with regards to size classes. A `UISplitViewController` "obeys" what size class it's showing as of - I think - iOS 13. Keeping in mind that you are (a) using an iPad, (b) using some version of iPadOS [you've *not* said], and (c) at the moment your two screenshots show "primary/secondary" [I think] and what I'll call "half/half" on "an iPad" [iPad mini? iPad pro 12.9 inch?, something else?] it's hard to actually duplicate. More, keep in mind that part of the question uses an Apple app [Calendar] that...??? –  Oct 03 '21 at 18:50
  • @dfd sorry, i am using an iPad Pro 11 inch with iPadOS 15. Would it help you if I created a repo? Could you take a look at it if I create it? – Niklas Oct 03 '21 at 19:05
  • I'd be happy to look at it. Two things. First, if I find something (code, etc.) that I think will help others by adding it to your question, I'll edit it in. Second, it will be about 14-16 hours before I'll get a chance to check it out. Post a link to your repo. Thanks. –  Oct 04 '21 at 02:07
  • @dfd no stress, thanks for your help: https://github.com/niklasgrewe/CalendarKit-iPadOS – Niklas Oct 04 '21 at 11:17
  • Hmph. I downloaded your project, built it using Xcode 13, ran it on an iPad mini running iPadOS 15.0.1. Portrait, landscape, even split screen. Worked fine. I chose an iPad mini because of how it handles split screen. The only "quirk" I see is that - when your app is in compact mode (pretty much always in portrait split screen and only when your app is one-third in landscape) I get your screenshot **of the iPhone** but no way to show the secondary view. (Hope that makes sense, I don't use Calendar much.) I'm looking at a screenshot I'll add to your question. –  Oct 04 '21 at 15:44
  • thats because you're are using the UIHostingController Version of the CalendarView. Please uncomment Line 35 in SceneDelegate for using native CalendarViewController and try again. You' will see my layout issues... – Niklas Oct 04 '21 at 16:00
  • Okay, why are you using `UIHostingController`? I may be missing something but that's used in SwiftUI, which you haven't tagged. I can show you how to simply use `UIKit` but it would involve changing `EventDetailView` into a `UIViewController`. If you are using SwiftUI, I'm probably wasting your time, sorry. –  Oct 04 '21 at 16:15
  • @dfd i only used `UIHostingController` because i couldn't find any other solution. If you know how I can fix the sidebar layout issues in UIKit, please show me. I have no idea... – Niklas Oct 04 '21 at 17:35
  • 1
    @dfd i also found this: https://hacknicity.medium.com/the-primary-view-controller-is-wider-than-what-is-visible-on-screen-to-provide-extra-content-when-ed00ca003acf – Niklas Oct 04 '21 at 17:49

1 Answers1

1

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

Niklas
  • 1,638
  • 4
  • 19
  • 48