2

I have a UIView class which I add to my main UIViewController and I need to check the orientation of the device (iPad) at the launch of the app, in the viewDidLoad method. However because the class is a UIView (not UIViewController) I can't use methods such as willAnimateRotationToInterfaceOrientation.

So I attempted to use this in my UIView class:

if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) ||
    ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {

However, testing with some breakpoints, whatever the orientation is, the of statement is never called, its skips right passed it. So what do you suggest I do to overcome this issue?

I need to somehow detect the orientation from the UIView class.

Thanks.

JOM
  • 8,139
  • 6
  • 78
  • 111
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253

2 Answers2

1

Where are you placing the check? The location could easily explain why it's not being called. To get rotation info, you could register for a notification, or have your view controller call a method in your view. Sample code for the latter:

// ViewController.m
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self.customView willRotateToOrientation:toInterfaceOrientation];
}

// CustomView.m
- (void)willRotateToOrientation:(UIInterfaceOrientation)newOrientation {
    // Handle rotation
}

The view controller method is one you override; the view's method should be declared in a header.

Update: Alternatively, you can find out the rotation in the controller's `viewWillAppear':

// ViewController.m
- (void)viewWillAppear {
    [self.customView willRotateToOrientation:[[UIDevice currentDevice] orientation];
}

The method in your view will be called accordingly.

Community
  • 1
  • 1
FeifanZ
  • 16,250
  • 7
  • 45
  • 84
  • I've tried this, trouble is, `willRotateToInterfaceOrientation` seems to be called after 'viewDidLoad' meaning that this is useless. So this method only works when rotating after a rotation and not as soon as the app loads up. – Josh Kahane Oct 15 '11 at 18:27
  • You could find out the orientation in `viewWillAppear` and make the call to the view then—see updated answer. – FeifanZ Oct 15 '11 at 18:34
  • Turns out that the link to the other answer worked well. `[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];` makes the if statement in my OP work a charm. – Josh Kahane Oct 15 '11 at 18:48
1

One thing you can to is to register for orientation notification from NSNotificationCenter:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];

...

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    // do things
}

This is however suboptimal since iPad may be laying flat on the table when app starts, and you'll get UIDeviceOrientationUnknown then. Been here, done that...

I ended up doing a trivial check like this:

BOOL landscape = self.bounds.size.width > self.bounds.size.height;
if (landscape)
    // landscape stuff
else
    // portrait stuff

But in my case the view changed aspect ratio upon rotation. If this is your case too, it should work fine.

ayoy
  • 3,835
  • 19
  • 20