Sheet not showing in landscape mode (UIKit)

Viewed 21

I'm a beginner trying to learn iOS.

I'm trying to show a sheet, but I'm finding it's not really showing anything.

This is what it looks like: Bug

This is my relevant code:

class ViewController: UIViewController {
    @IBAction func openMemoryLog(_ sender: UIButton) {
        let memoryLogViewController = MemoryLogViewController()
        present(memoryLogViewController, animated: true)
    }
}

class MemoryLogViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        print("Memory log loaded")
        
        if let presentationController = presentationController as? UISheetPresentationController {
            presentationController.detents = [ .medium() ]
        }
    }
}

enter image description here

I can see the print statement going through, so it's at least loading. But don't know what's going on here. Looks like some sort of modal thing is happening, but the IB interface isn't showing.

1 Answers

When you use Storyboard / IB to design an interface, you have to instantiate that controller from the Storyboard - you cannot simply assign the class to a variable.

@IBAction func openMemoryLog(_ sender: UIButton) {
    // if you designed MemoryLogViewController in Storyboard,
    //  you cannot create it like this:
    //let memoryLogViewController = MemoryLogViewController() <-- wrong
    
    // you need to instantiate it from the Storyboard
    //  for example, if you gave your controller a Storyboard ID of "memoryLogViewController":
    if let memoryLogViewController = storyboard?.instantiateViewController(withIdentifier: "memoryLogViewController") {
        present(memoryLogViewController, animated: true)
    }
}
Related