0

I'm trying to present UINavigationController as a small window using the following way. It works fine on iPads but it still displays fullScreen on iPhones. Any tips on what I am doing wrong will be greatly appreciated!

class LoginNewNavigationController: UINavigationController{
    
    private var windowSize: CGSize!
    
    init() {
        let rootVc = LoginNewIPadViewController.init()
        super.init(rootViewController: rootVc)
        
        self.configureSizes()
        self.preferredContentSize = self.windowSize
        self.modalPresentationStyle = .formSheet
        self.showNavigationBar = false
        self.view.backgroundColor = .clear
        self.view.layer.cornerRadius = 25
    }
}
isuru
  • 3,385
  • 4
  • 27
  • 62

3 Answers3

1

A "smaller window" presentation is not built in on iPhone. You need a custom presentation controller that dictates a smaller size and placement of the presented view controller.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

It's the LoginnewIPadViewController that needs to have it's modal presentation style set not the navigation controller. Also there is no need to reference self the scope is clear for the compiler.

class LoginNewNavigationController: UINavigationController{

private var windowSize: CGSize!

    init() {
        let rootVc = LoginNewIPadViewController.init()
        rootVc.modalPresentationStyle = .formSheet
        super.init(rootViewController: rootVc)
        configureSizes()
        preferredContentSize = windowSize
        showNavigationBar = false
        view.backgroundColor = .clear
        view.layer.cornerRadius = 25
    }
}
andromedainiative
  • 4,414
  • 6
  • 22
  • 34
0

Not possible with a formSheet on iPhone, it can be achieved using .popover style. See following example -

class LoginNewIPadViewController: UIViewController {}

class LoginNewNavigationController: UINavigationController {
    
    private var windowSize: CGSize!
    
    init() {
        let rootVC = LoginNewIPadViewController.init()
        super.init(rootViewController: rootVC)
        
        self.preferredContentSize = self.windowSize
        self.modalPresentationStyle = .popover
        
        if let presentationController = self.popoverPresentationController {
            /// Configure the way you want this to be shown
            // presentationController.sourceRect = ??
            // presentationController.sourceView = ??
            // presentationController.barButtonItem = ??
            presentationController.permittedArrowDirections = .up
            presentationController.delegate = self
        }
    }
    
}

extension LoginNewNavigationController: UIPopoverPresentationControllerDelegate {
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
    }
}
Tarun Tyagi
  • 9,364
  • 2
  • 17
  • 30