Search code examples
ios7rubymotion

Rubymotion UIView Orientation


I currently have an ios app that I am writing in Rubymotion. I am trying to setup a UIViewController to display in portrait orientation always and not rotate to landscape. I cannot only specify portrait orientation in my rakefile as I need all orientaitons for other uiviewcontrollers. See below for my code:

class ConfirmationController < UIViewController

def viewDidLoad
    super
    self.view.backgroundColor = UIColor.blueColor
end

def shouldAutorotate
  true
end

def supportedInterfaceOrientations
  UIInterfaceOrientationMaskPortrait
end

def preferredInterfaceOrientationForPresentation
  UIInterfaceOrientationMaskPortrait
end

As you can see I am trying to set my preferredInterfaceOrientation but it is still changing to a landscape orientation when my device is rotated. Any ideas on how to set this up with Rubymotion?


Solution

  • After doing research I found where the issue came from was the UINavigationController being the rootView. I had to add a named controller that inherited from UINavigationController and then override the default UINavigation settings to change according to topViewController.

    AppDelegate.rb

    class TopNavController < UINavigationController
    
      def supportedInterfaceOrientations
        self.topViewController.supportedInterfaceOrientations
      end
    
      def preferredInterfaceOrientationForPresentation
        self.topViewController.preferredInterfaceOrientationForPresentation
      end
    end
    
    main_controller = MainScreenController.alloc.initWithNibName(nil, bundle: nil)
    @window.rootViewController= TopNavController.alloc.initWithRootViewController(main_controller)
    

    UIViewController

    def shouldAutorotate
      true
    end
    
    def supportedInterfaceOrientations
      UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight
    end
    
    def preferredInterfaceOrientationForPresentation
      UIInterfaceOrientationLandscapeLeft
    end