ios6 autorotation portraitupsidedown to portrait

时间:2012-12-04 13:38:01

标签: ios uiviewcontroller ios4 auto-rotation

请不要建议我在ios 4.3-6.0中进行一次旋转是一个坏主意,因为我多次告诉并且没有听我说。

在项目设置中,我已经设置了所有支持的界面方向,只是为了确保。

现在我正在用iphone4测试ios6。从app delegate开始,它是一个代码,如下所示:

mainController = [[MainViewController alloc] initWithNibName:nil bundle:nil];

navigationController=[[RotatingNavigationController alloc] initWithRootViewController:mainController];          
navigationController.navigationBar.hidden =true;

// ios: 4 and 5
//[window addSubview:navigationController.view];

//ios6:
window.rootViewController = navigationController;

[window makeKeyAndVisible];

所以我做了2个自定义类,在自动旋转的情况下推荐这些类,并且问题已经解决了。

RotatingNavigationController是一个自定义的UINavigationController有一些丑陋的代码,但是我已经从这个论坛中收集了这些部分,所以应该允许发布它:

@implementation RotatingNavigationController

- (id)init {
    self = [super init];
    if (self) {        
        self.view.autoresizesSubviews = YES;
        [self.view setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
    }
    return self;
}

// ios6 require this method for rotation detection, available from ios5, ios4.3 not available: sux
//- (void)viewWillLayoutSubviews
//{
    //UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation;

    //[[NSNotificationCenter defaultCenter]     postNotificationName:UIDeviceOrientationDidChangeNotification     object:nil];

//    [[NSNotificationCenter defaultCenter] addObserver:self                                              selector:@selector(orientationChanged:)                                                  name:UIDeviceOrientationDidChangeNotification                                                object:nil];

    /*
    if(UIInterfaceOrientationIsLandscape(statusBarOrientation)){
        [self willRotateToInterfaceOrientation: statusBarOrientation duration: 0.3 ];
    }
    else{
        [self willRotateToInterfaceOrientation: statusBarOrientation duration: 0.3 ];
    }
    */
//}

//ios 4.3 and ios 5.0, at ios6 it isn't called by iOS...sux
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [self.visibleViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}

// //ios 4.3 and ios 5.0, at ios6 need different methods, which sux
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
} 

//Autorotation is changing in iOS 6. In iOS 6, the shouldAutorotateToInterfaceOrientation: method of UIViewController is deprecated. In its place, you should use the supportedInterfaceOrientationsForWindow: and shouldAutorotate methods.


// ios6
- (NSUInteger)supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskAll;
}

// ios6
- (BOOL)shouldAutorotate
{
    return YES;
}
// ios6
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationMaskPortrait;
}
@end
  • 这应该正确地生成自动旋转通知,我认为他完成了他的工作。

MainViewController是自定义UIViewController

只是为了确保我已经复制粘贴代码:

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    //    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return YES;
}

// ios6:
- (NSUInteger)supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskAll;
}

// ios6
- (BOOL)shouldAutorotate
{
    return YES;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationMaskPortrait;
}

他的工作是根据设备状态(旋转)更改不同的UIViewController

我有一个菜单屏幕,它有不同的xib,但也有h和m文件(因为有不同的元素,它们没有出现在Portait或Landscape中。(我没有权力改变整个架构)

代码的一部分 - 这里应该是一些问题,我想在下面这个Maincontroller:

#pragma mark
#pragma mark Rotation
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation; 

    if(deviceOrientation == UIDeviceOrientationFaceUp || deviceOrientation == UIDeviceOrientationFaceDown){
        if(debug){
            NSLog(@"Nothing to change because it is gone to Flat");
        }
        return;
    }
NSArray *viewControllers = [self.navigationController viewControllers];

if (UIDeviceOrientationIsLandscape(deviceOrientation)){

        //if(debug)
        //    NSLog(@"Portait -> Landscape size : %3.0fw, %3.0fh", self.view.bounds.size.width, self.view.bounds.size.height);
        id lastviewController = viewControllers.lastObject;
        // check what is there:
        if([lastviewController isKindOfClass:[MenuController class]]){                 
            [self.navigationController popViewControllerAnimated:NO];
            [self.navigationController pushViewController:menuLandscape animated:NO];
            NSLog(@"poped Menu Portrait, pushed Menu Landscape");
        }
...
else if(UIDeviceOrientationIsPortrait(deviceOrientation)){
    //else if(UIInterfaceOrientationIsLandscape(statusBarOrientation)){

        //if(debug)
        //    NSLog(@"Landscape -> Portait , size : %3.0fw, %3.0fh", self.view.bounds.size.width, self.view.bounds.size.height);
        id lastviewController = viewControllers.lastObject;
        // check what is there:
        if([lastviewController isKindOfClass:[MenuLandscapeController class]]){
            [self.navigationController popViewControllerAnimated:NO];            
            [self.navigationController pushViewController:menuPortait animated:NO];
        } 
...

问题是:景观被取出并被​​推倒,当倒置时,但是那些画面没有旋转,它显示出破损的布局。

如何唤醒该控制器并告诉他是时候旋转,因为它不处于纵向模式?

感谢任何与我的问题相关的建议,除了架构变更之外的任何改进。

更新

在app delgate的

没有帮助添加:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationMaskAll;
}

来自其他答案的代码:presented here

UPDATE2:

UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation; 

出于某种原因不想取UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown值,我认为需要解决。

UPDATE3:   仔细阅读两次,第三次ios6 release notes

  

系统确定是否支持方向   与应用程序返回的值相交   supportedInterfaceOrientationsForWindow:带值的方法   由最顶层的supportedInterfaceOrientations方法返回   全屏控制器。

  • 现在再读一遍:)

1 个答案:

答案 0 :(得分:4)

将此类别添加到不旋转的viewcontroller

@implementation UINavigationController(Rotation)

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

@end