iOS 5: willRotateToInterfaceOrientation:duration not called when first loading controller

后端 未结 4 1155
花落未央
花落未央 2020-12-16 04:23

I\'ve implemented this method in my code to know when an interface orientation change will occur:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrient         


        
相关标签:
4条回答
  • 2020-12-16 04:35

    Since I'm pressed for time I've had to work around this odd behaviour and manually call the orientation methods in viewDidLoad. This is a bit of a hack but it's working fine so far.

    In viewDidLoad I added this:

    if ( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0") ) {
        UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
        [self willRotateToInterfaceOrientation:interfaceOrientation duration:0.2f];
    }
    

    I've then added this in a common header file:

    // iOS Version Checking
    #define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
    #define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
    #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
    #define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
    #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
    

    I got the iOS version checking defines from an answer to this question on SO.

    0 讨论(0)
  • 2020-12-16 04:44

    Since iOS 5 and higher you should build you view controllers hierarchy (using -addChildViewController:). Then child view controllers begin to call -willRotateToInterfaceOrientation:interfaceOrientation: and other interface related methods again (because parent controller will notify them about UI orientation changes).

    Please, do not use any 'dirty' methods.

    Useful documentation: Apple docs about 'addChildViewController:'

    0 讨论(0)
  • 2020-12-16 04:47

    I've run into this same problem and have been pulling my hair out. I've finally discovered a solution but unfortunately this will be iOS 5.0 + only:

    Place the following code in your viewDidLoad method in your custom UIViewController:

    double delayInSeconds = 0.1f;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [UIViewController attemptRotationToDeviceOrientation];
    });
    

    This will push the rotation update to the next run loop. attemptRotationToDeviceOrientation is an iOS 5.0+ call.

    0 讨论(0)
  • 2020-12-16 04:48

    Even I faced a similar issue and had to override it in the tabbarcontroller derived class to get this to work:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        [_tabBarConroller willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
        [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    }
    

    Hope this helps!!

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