UIDevice currentDevice's “orientation” always null

后端 未结 5 1742
一生所求
一生所求 2020-12-18 00:23

As per the title. Calling [[UIDevice currentDevice] BeginGeneratingDeviceOrientationNotifications] has no effect.

DidRotateToInterfaceOrientation etc events are wor

相关标签:
5条回答
  • 2020-12-18 00:27

    seems like a silly question, but isn't it

    beginGeneratingDeviceOrientationNotifications

    ( lower case b ) ...

    0 讨论(0)
  • 2020-12-18 00:31

    this is as per iMeMyself said in the comments above - this samed me a lot of time and I think is the right answer so I wanted to highlight it here:

    UIDeviceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
    
    if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
    {
       //do stuff here
    }
    else if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
    {
    //or do stuff here
    }
    
    0 讨论(0)
  • 2020-12-18 00:39

    If you check [[UIDevice currentDevice] orientation] in - (void)viewDidLoad you will always get nil.

    Check it in *- (void) viewDidAppear:(BOOL)*animated method and you

    0 讨论(0)
  • 2020-12-18 00:41

    UIDevice's notion of orientation seems to be only available on actual devices. The simulator seems to always return 0 here, regardless of whether the notifications have been enabled as the docs suggest. Irritatingly inconvenient, but there you go.

    I find this works fine on the actual device:

        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        NSLog(@"orientation: %d", [[UIDevice currentDevice] orientation]);
        [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    
    0 讨论(0)
  • 2020-12-18 00:41

    Wouldn't [[UIDevice currentDevice] orientation] give you the current orientation state? You could check for this in your viewWillAppear method of the view controller that wants to poll.

    Edit: Other than that, there are various ways to get the current orientation, such as using the statusBarOrientation property in UIApplication, or interfaceOrientation property in UIViewcontroller.

    0 讨论(0)
提交回复
热议问题