Search code examples
iosuiviewuiviewcontrolleruikitswift-framework

Disable screen rotation of a xib in swift


I created a dynamic framework in swift wherein a .xib file inside my framework opens up whenever a voip notification lands. I want to restrict the orientation of the screen to portrait mode only, but i am not sure how to do it.

I tried below methods but these can only be used in a UIViewController and not in UIView.

override var shouldAutorotate: Bool {
    return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

Solution

  • After so long i finally figured out the solution. I created a view controller class in my framework from which i am opening the .xib file. I included the following function in my newly create view controller

    override var shouldAutorotate: Bool {
        return true
    }
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    

    now before opening the .xib i present this viewcontroller over the top of the current view controller i.e i called this viewcontroller from its own file, somewhat like this

    self.navCtrl?.present(self, animated: false, completion: nil)

    by doing so my .xib file is opened above my framework's viewcontroller rather than the app's view controller. to dismiss this viewcontroller, simply use

    dismiss(animated: false, completion: nil)
    

    Hence problem solved.