Search code examples
iosswiftipaduisplitviewcontrollerbeta

Is this an iOS 14 UISplitViewController bug?


In my app, I want a three-column UISplitViewController. I create it like this:

let svc = UISplitViewController(style: .tripleColumn)
svc.preferredDisplayMode = .twoOverSecondary
svc.setViewController(TestViewController(), for: .primary)
svc.setViewController(TestViewController(), for: .supplementary)
svc.setViewController(TestViewController(), for: .secondary)
svc.primaryBackgroundStyle = .sidebar

This ViewController that I'm presenting is a really simple viewController. It doesn't do anything except, present a centered red square.

class TestViewController: UIViewController {
    private var redView = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        redView.backgroundColor = .red
        redView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(redView)
        redView.widthAnchor.constraint(equalToConstant: 30).isActive = true
        redView.heightAnchor.constraint(equalToConstant: 30).isActive = true
        redView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        redView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
    }
}

Now, when I run this code, I get the following result.

enter image description here

As you can see, 2 of the 3 squares are off-center. Anyone know, what I'm doing wrong here? Or is this a known bug?


Solution

    1. You should use "Safe Area" instead of "self.view" when setting to center.
    2. The detail view here has a push-away effect. You just need to click on it and the primary view controller will hide, like below.

    enter image description here