Get launch orientation of iPad app

后端 未结 9 647
无人及你
无人及你 2020-12-02 16:19

In my iPad app, I need to run some layout code to set the proper layout depending on the orientation. By default, the layout is configured for the landscape orientation, so

相关标签:
9条回答
  • 2020-12-02 16:44

    Not that you need another answer, but I thought I should add that you almost never want to use [[UIDevice currentDevice] orientation]. That method returns the orientation of the device, which isn't necessarily the same as the orientation of the interface.

    0 讨论(0)
  • 2020-12-02 16:45

    Use self.interfaceOrientation in your view controller - it's a property of UIViewController that is set by iOS for you, and in some cases is more reliable than [[UIDevice currentDevice] orientation].

    Here's a detailed description: http://bynomial.com/blog/?p=25

    0 讨论(0)
  • 2020-12-02 16:45

    As mentioned in a blog post above, there is a set of macros for testing orientation. That blog post however mentions UIDeviceOrientationIsPortrait. I like the following below, it's a minor twist.

    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
    {
      NSLog(@"Portrait");
    }
    else
    {
      NSLog(@"Landscape");
    }
    

    An observation I've made is that you can't call this code in a table view, pushed on to a Navigation Controller embedded in the split view controller. So in other words you can't call it from the master view controller. You have to replace the "self.interfaceOrientation" with splitviewcontroller.interfaceOrientation, assuming you maintain a reference to the parent split view controller.

    0 讨论(0)
提交回复
热议问题