开发者

UIViewController device rotation delegate methods are not getting called in iOS5

I am developing an iPhone application. In this application, UIViewController (vc1) presents another UIViewController (vc2). vc1 supports both Portrait and Landscape orientations; vc2 supports only Portrait orientation.

  1. When vc2 is presented, it asks vc1: shouldAutorotateToInterfaceOrientation: and this returns YES
  2. In iOS5 (Beta 7) willRotateToInterfac开发者_StackOverflow社区eOrientation:, didRotateFromInterfaceOrientation: are not getting called for this sequence. But, this works fine in iOS4. Is this a bug in iOS5?


I had reported a bug to Apple and I got the following reply:

"Engineering has determined that this issue behaves as intended based on the following information:

The presentation behavior is correct - if it behaved differently in previous versions, that was a bug. The arguably unexpected change in behavior regards the dismiss of VC1 which no longer gets rotation callbacks but it will layout in portrait.

There are other ways to determine what your orientation is when a view controller lays itself out. For various reasons, relying on the rotation callbacks proved to be problematic.

In general, viewController rotation callbacks occur in two cases:

  1. The device orientation changes for view controllers in the window hierarchy
  2. Mixed interface orientation presentations. (Bottom controller only supports portrait, device is in landscape, and a view controller that supports landscape is presented.) However this is arguably a misfeature.

Try using viewWillLayoutSubviews: in iOS 5."


I had faced a similar issue when testing my app on iOS5. The layout of subviews in the main view controller used to get messed up if the orientation changed when a modal view controller was active.

What I did was to store the current orientation flag in the main controller. This flag is updated in two places in the main controller

  1. willAnimateRotationToInterfaceOrientation:
  2. viewWillLayoutSubviews (this is on iOS5 only)

I write all the logic to adjust the subviews by comparing the current orientation with the stored value. If they are different - update the stored orientation, update your subviews.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{    
    // Any orientation is OK
    return YES;
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    portrait = UIInterfaceOrientationIsPortrait(toInterfaceOrientation);

    // Code to update subview layout goes here
}

-(void)viewWillLayoutSubviews
{
    BOOL isPortraitNow = UIInterfaceOrientationIsPortrait(self.interfaceOrientation);

    if(isPortraitNow != portrait)
    {        
        DLog(@"Interfaceorientation mismatch!, correcting");

        portrait = isPortraitNow;

        // Code to update subview layout goes here
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜