Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDeviceOrientation ipad

Tags:

ios

ipad

I have the following code to fill a view according to the orientation. This always returns landscape.

- (void)setData:(BCPlaylist *)list  {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown){
      NSLog(@"portrait");
      [self setPlaylist:list];
      [self renderPlaylist];
      [activity stopAnimating];
    }else{
      NSLog(@"landscape");
      [self setPlaylist:list];
      [self renderPlaylistOne];
      [activity stopAnimating];
     }
}

I change views correctly in - (void)animateRotation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { But that doesn't work when already in landscape or portrait when changing a playlist.

like image 707
MaikelS Avatar asked Sep 14 '11 08:09

MaikelS


3 Answers

Instead of using [[UIDevice currentDevice] orientation] for checking orientation use statusbar orientation to get the exact orientation.

    UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
like image 89
alloc_iNit Avatar answered Nov 15 '22 06:11

alloc_iNit


To avoid a warning, use :

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
like image 35
Samuel De Backer Avatar answered Nov 15 '22 04:11

Samuel De Backer


You'd always get landscape, because that's your default (from your if statement). If you walk through the debugger with a breakpoint there, you'll see that the orientation reported is that of Unknown.

In fact, your code is fine, but this is a limitation of the simulator. If you take the same code, using Device orientation and not Interface orientation, you'll get actual values if you use it on the physical device, which can be driven by the if-statement you have.

like image 33
idStar Avatar answered Nov 15 '22 05:11

idStar