0

I am detecting the device orientation with these lines of code:

UIDevice.currentDevice().orientation.isPortrait.boolValue
UIDevice.currentDevice().orientation.isLandscape.boolValue

And I detect check if it's an iPad with this code:

userInterfaceIdiom == .Pad

I used this code in commonInit() function which is called in the Init() function. When I execute my code on iPad the two first lines return both false which is not correct, one of them should be true. The third line is working fine.

If I use the code in other functions, such as supportedInterfaceOrientations(), it works fine. Do you know what could be the problem?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user567
  • 3,712
  • 9
  • 47
  • 80
  • 2
    `init()`... `init()`..., whose `init()` method are we talking about? – holex Sep 06 '16 at 15:23
  • The init() methode of the classe where I am trying to use the lines of code. I want to inantiate the class with values dependent of the orientation of the iPad – user567 Sep 06 '16 at 15:33
  • @holex was asking for the *type* of the class...is it a UIViewController? Has the App fully initialized and told the AppDelegate it's ready before this class is constructed? – BaseZen Sep 06 '16 at 18:03

1 Answers1

1

You are probably neglecting this documentation:

You also use the UIDevice instance to detect changes in the device’s characteristics, such as physical orientation. You get the current orientation using the orientation property or receive change notifications by registering for the UIDeviceOrientationDidChangeNotification notification. Before using either of these techniques to get orientation data, you must enable data delivery using the beginGeneratingDeviceOrientationNotifications method. When you no longer need to track the device orientation, call the endGeneratingDeviceOrientationNotifications method to disable the delivery of notifications.

Also, this is a 3-D orientation. You probably don't want that. What is the actual value of simply the orientation property? See:

enum UIDeviceOrientation : Int {
    case Unknown
    case Portrait
    case PortraitUpsideDown
    case LandscapeLeft
    case LandscapeRight
    case FaceUp
    case FaceDown }

Probably what you really want is here: "interfaceOrientation" is deprecated in iOS 8, How to change this method Objective C

Sometimes TraitCollections doesn't fill all your design needs. For those cases, Apple recommends to compare view's bounds :

if view.bounds.size.width > view.bounds.size.height { // ... }

Community
  • 1
  • 1
BaseZen
  • 8,650
  • 3
  • 35
  • 47