iOS: Device orientation on load

前端 未结 16 1950
遥遥无期
遥遥无期 2020-11-28 23:45

It seems that when my app loads, it does not know its current orientation:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (o         


        
16条回答
  •  青春惊慌失措
    2020-11-29 00:16

    This is the real answer. When an app starts up, it's orientation is unknown. It uses shouldAutorotateToInterfaceOrientation and supportedInterfaceOrientations to decide which orientation to pick.

    Watch as I start up a sample app in the iPhone 5.0 simulator and rotate it using the code below and 'Supported interface orientations' with all 4 possible orientations:

    20:44:08.218 RotationTestApp Supported orientation: Portrait
    20:44:08.222 RotationTestApp Supported orientation: Portrait (upside-down)
    20:44:08.225 RotationTestApp Supported orientation: Landscape (home button on the right)
    20:44:08.225 RotationTestApp Supported orientation: Landscape (home button on the left)
    20:44:08.226 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
    20:44:08.237 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
    20:44:08.239 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
    20:44:08.240 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
    20:44:09.817 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeLeft)
    20:44:09.833 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeLeft)
    20:44:11.030 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
    20:44:11.040 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
    20:44:12.599 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeRight)
    20:44:12.609 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeRight)
    20:44:13.301 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
    

    I've seen lots of code snippets, but none of them work generically enough (iPad & iPhone, iOS 5.0+).

    Instead of fumbling around with try-this-try-that, place the following in your root vc:

    #define ToNSString_BEGIN(T) \
    NSString* T##ToNSString(T valueParameter) { \
    switch (valueParameter) {
    
    #define ToNSString_VALUE(value) \
    case value: return @#value
    
    #define ToNSString_END(T) \
    } \
    return @"(unknown)"; \
    }
    
    // NSString* UIInterfaceOrientationToNSString(UIInterfaceOrientation);
    ToNSString_BEGIN(UIInterfaceOrientation);
    ToNSString_VALUE(UIInterfaceOrientationPortrait);           // 1
    ToNSString_VALUE(UIInterfaceOrientationPortraitUpsideDown); // 2
    ToNSString_VALUE(UIInterfaceOrientationLandscapeLeft);      // 3
    ToNSString_VALUE(UIInterfaceOrientationLandscapeRight);     // 4
    ToNSString_END  (UIInterfaceOrientation);
    
    // NSString* UIDeviceOrientationToNSString(UIDeviceOrientation);
    ToNSString_BEGIN(UIDeviceOrientation);
    ToNSString_VALUE(UIDeviceOrientationUnknown);               // 0
    ToNSString_VALUE(UIDeviceOrientationPortrait);              // 1
    ToNSString_VALUE(UIDeviceOrientationPortraitUpsideDown);    // 2
    ToNSString_VALUE(UIDeviceOrientationLandscapeLeft);         // 3
    ToNSString_VALUE(UIDeviceOrientationLandscapeRight);        // 4
    ToNSString_VALUE(UIDeviceOrientationFaceUp);                // 5
    ToNSString_VALUE(UIDeviceOrientationFaceDown);              // 6
    ToNSString_END  (UIDeviceOrientation);
    
    
    
    // Change this custom method to alter auto-rotation behavior on all supported iOS versions and platforms.
    - (BOOL)allowAutoRotate:(UIInterfaceOrientation)interfaceOrientation
    {
        NSUInteger interfaceOrientationAsMask = (1<

    There is still a nagging problem of segue animations not using the current orientation. My guess is that subclassing each VC and putting some orientation on push / notify delegate on pop would be the way to go.

    Also important:

    shouldAutorotateToInterfaceOrientation doesn't work

    tabBarController and navigationControllers in landscape mode, episode II

提交回复
热议问题