简体   繁体   中英

UIDeviceOrientation 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.

而不是使用[[UIDevice currentDevice] orientation]来检查方向,使用状态栏方向来获得精确的方向。

    UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

要避免警告,请使用:

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM