UIDeviceOrientation ipad

I have the following code to populate the view according to orientation. It always brings back the 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 am correctly changing the views in - (void)animateRotation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { But this does not work when you are already in landscape or portrait when changing the playlist.

+6
source share
3 answers

Instead of orientation [[UIDevice currentDevice]] to check the orientation, use the orientation of the status bar to get the exact orientation.

  UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
+8
source

To avoid a warning, use:

 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
+2
source

You will always have a landscape, because this is your default value (from your if statement). If you go through the debugger with a breakpoint, you will see that the orientation specified is unknown.

In fact, your code is fine, but this is a simulator limitation. If you take the same code using device orientation rather than interface orientation, you will get actual values ​​if you use it on a physical device, which can be controlled using the if statement.

+1
source

Source: https://habr.com/ru/post/897290/


All Articles