8

I'm developing an iOS application with latest SDK.

I have set Landscape right orientation as the unique orientation available and I have a question about viewController view.

This is my code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(deviceOrientationDidChange:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != AVCaptureVideoOrientationLandscapeRight);
}

- (void)deviceOrientationDidChange:(NSNotification*)notification
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == AVCaptureVideoOrientationLandscapeLeft)
        NSLog(@"Orientation: Landscape Left");
    else if (orientation == AVCaptureVideoOrientationLandscapeRight)
        NSLog(@"Orientation: Landscape Right");
    NSLog(@"FRAME: %@", NSStringFromCGRect(self.view.frame));
}

And this is the log that I get:

Orientation: Landscape Right
FRAME: {{0, 0}, {320, 568}}
Orientation: Landscape Right
FRAME: {{0, 0}, {320, 568}}

My question is about {{0, 0}, {320, 568}}:

Why am I getting these values?

I think the correct values would be {{0, 0}, {568, 320}}.

VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • my guess is its a fullscreen videocontroller you are displaying, while the app is still in portrait. – Nitin Alabur Mar 19 '13 at 15:38
  • I have tested the program with simulator and it appears on landscape right orientation. And yes, it is a full screen app. – VansFannel Mar 19 '13 at 15:40
  • You shouldn't be listening for device orientation changes. `UIViewController` has proper methods for listening to interface orientation changes. Add your logging code in those proper methods. Try `didRotateFromInterfaceOrientation:` or `viewDidLayoutSubviews`. – rmaddy Mar 19 '13 at 16:14
  • @rmaddy Thanks for your comment but I'm getting the same output. – VansFannel Mar 19 '13 at 17:11

4 Answers4

3

I believe that the frame rects don't react to orientation change.

EDIT: My original solution was overly complicated for your needs. If you're correctly finding the current orientation, then just interpret the width of the rect as the height if you're in landscape mode.

drewmm
  • 737
  • 6
  • 17
  • That's what I think. Thanks for your answer. – VansFannel Mar 19 '13 at 17:14
  • 1
    Take a look to the answer for this question: http://stackoverflow.com/questions/2686882/incorrect-frame-window-size-after-re-orientation-in-iphone. It's better to use bounds instead of frame. – VansFannel Mar 19 '13 at 17:21
  • 1
    Ah, very cool! I didn't find that answer when I was searching on this issue. I'll go back to my code, and see if this works there. It's definitely more elegant. – drewmm Mar 19 '13 at 17:29
1

I add this solution to show what happens with frame and bound.

I've added these three methods:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
        CGRect bounds = [view bounds];
        NSLog(@"FRAME: %@", NSStringFromCGRect(view.frame));
        NSLog(@"BOUNDS: %@", NSStringFromCGRect(bounds));
}

- (void)deviceOrientationDidChange:(NSNotification*)notification
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == AVCaptureVideoOrientationLandscapeLeft)
        NSLog(@"1. Orientation: Landscape Left");
    else if (orientation == AVCaptureVideoOrientationLandscapeRight)
        NSLog(@"1. Orientation: Landscape Right");
    NSLog(@"1. FRAME: %@", NSStringFromCGRect(self.view.frame));
    NSLog(@"1. BOUNDS: %@", NSStringFromCGRect(self.view.bounds));
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == AVCaptureVideoOrientationLandscapeLeft)
        NSLog(@"2. Orientation: Landscape Left");
    else if (orientation == AVCaptureVideoOrientationLandscapeRight)
        NSLog(@"2. Orientation: Landscape Right");
    NSLog(@"2. FRAME: %@", NSStringFromCGRect(self.view.frame));
    NSLog(@"2. BOUNDS: %@", NSStringFromCGRect(self.view.bounds));
}

- (void)viewDidLayoutSubviews
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == AVCaptureVideoOrientationLandscapeLeft)
        NSLog(@"3. Orientation: Landscape Left");
    else if (orientation == AVCaptureVideoOrientationLandscapeRight)
        NSLog(@"3. Orientation: Landscape Right");
    NSLog(@"3. FRAME: %@", NSStringFromCGRect(self.view.frame));
    NSLog(@"3. BOUNDS: %@", NSStringFromCGRect(self.view.bounds));
}

And this is what I get:

FRAME: {{0, 0}, {320, 568}}
BOUNDS: {{0, 0}, {320, 568}}
3. Orientation: Landscape Right
3. FRAME: {{0, 0}, {320, 568}}
3. BOUNDS: {{0, 0}, {568, 320}}
1. Orientation: Landscape Right
1. FRAME: {{0, 0}, {320, 568}}
1. BOUNDS: {{0, 0}, {568, 320}}
1. Orientation: Landscape Right
1. FRAME: {{0, 0}, {320, 568}}
1. BOUNDS: {{0, 0}, {568, 320}}

Bound changes on viewDidLayoutSubviews:.

VansFannel
  • 45,055
  • 107
  • 359
  • 626
0

Use self.view.bounds instead of self.view.frame

Hsm
  • 1,510
  • 17
  • 16
0

Please change change on your viewController

 SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];  

replace by

 SKScene * scene = [MyScene sceneWithSize:CGSizeMake(skView.frame.size.height, skView.frame.size.width)];
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364