Search code examples
iosrubymotion

Changing device orientation settings by Device in Rubymotion


I have an universal app using Rubymotion, and the UI setup differs greatly between iPhone version and iPad version.

The main being, in iPhone it only supports portrait, and iPad only supports landscape.

I want to do this, if possible by setting something up on the Rakefile, from what I understand the alternative would be to do something in the delagate method, but if it where possible I would like to set orientation settings in the settings file.

Motion::Project::App.setup do |app|
  #app settings
  app.name = 'xxxxx'
  app.version = "0.xxxx"
  app.device_family = [:iphone, :ipad]
  app.interface_orientations = [:portrait]
end

EDIT:

some updates on how Jamon's answer worked out. having shouldAutorotate return false in the UINavigation resulted in odd occurrences on the iPad version, having parts of the iPad version show its view content as portrait even though the orientation is set to landscape, it worked out when I returned true for shouldAutorotate.


Solution

  • You won't be able to do that in your Rakefile, as far as I know. You need to specify that you provide both orientations and then programmatically tell iOS whether an orientation is supported or not.

    Your UIViewControllers and/or UINavigationController(s) should look like this:

    def ipad?
      NSBundle.mainBundle.infoDictionary["UIDeviceFamily"].include?("2")
    end
    
    def shouldAutorotate
      false # I think?
    end
    
    def supportedInterfaceOrientations
      if ipad?
        UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight
      else
        UIInterfaceOrientationMaskPortrait
      end      
    end
    
    def preferredInterfaceOrientationForPresentation
      ipad? ? UIInterfaceOrientationMaskLandscapeLeft : UIInterfaceOrientationMaskPortrait
    end
    

    Haven't tested this code, but I've used similar in the past. You can use ternary (like I did in the last method) or a regular if.