abhimuralidharan
abhimuralidharan

Reputation: 5939

How to present a landscape only ViewController from portrait bottom of iPhone in swift?

I have portrait only app with one screen in landscape only mode.

What I did: I created a UINavigationController subclass and overridden the following:

import UIKit

class LandscapeVCNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscape
    }
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .landscapeLeft
    }
}

Then using this navigationController to present my landscape viewcontroller like this:

if let vc = storyboard?.instantiateViewController(withIdentifier: StoryBoardIdentifiers.playerVCStoryboardIdentifier) as? PlayerViewController {

            let navController = LandscapeVCNavigationController.init(rootViewController: vc)

            present(navController, animated: true, completion: {
            })
        }

This works fine.

What I need:

I need the landscape VC to be presented from the bottom(home button side) and should dismiss to the bottom(home button side). This is what happens currently.

enter image description here

Upvotes: 5

Views: 1235

Answers (1)

Harikrishnan R
Harikrishnan R

Reputation: 150

You have to add a custom transition animation to change this default behaviour.

    let transition = CATransition()
    transition.duration = 0.3
    transition.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromLeft
    self.view!.window!.layer.add(transition, forKey: nil)
    self.present(navController, animated: false, completion:nil) 

Upvotes: 1

Related Questions