Drawing issues using SwiftUI view as a PrimaryView in uisplitviewcontroller

59 views Asked by At

I am trying to use SwiftUI View as a primary view in a UISplitViewController. I have setup and added my PrimaryViewController to the NavigationController in my SplitviewController, in Storyboard.

mySwiftUI view code is as follows:

struct Tile_SwiftUI: View {
    var body: some View {
        RoundedRectangle(cornerRadius: 15)
            .fill( Color.red )
            .frame(height: 100)
            .padding()
     }
}

And my PrimaryViewController setup is as follows:

import UIKit
import SwiftUI

class PrimaryViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let testVC = UIHostingController(rootView: Tile_SwiftUI() )
        self.view.backgroundColor = UIColor.white
        testVC.view.backgroundColor = UIColor.blue
        addChild(testVC)

        view.addSubview(testVC.view)
        testVC.view.translatesAutoresizingMaskIntoConstraints = false
        
            NSLayoutConstraint.activate([
                testVC.view.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor ),
                testVC.view.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor ),
                testVC.view.topAnchor.constraint(equalTo: view.topAnchor ),
                testVC.view.bottomAnchor.constraint(equalTo: view.bottomAnchor ),
            ])
        testVC.didMove(toParent: self)

    }
 }

It all works as expected on the iPhone and iPad landscape orientation, but on portrait orientation the primaryViewController gets an odd overlay. Please see attached images.

enter image description here enter image description here enter image description here

Here is also a link to the full project:

https://www.sticklets.co/code/SplitViewController_SwiftUI_bug.zip

Any ideas what is causing this?

0

There are 0 answers