ios6 autorotation portraitupsidedown to portrait [英] ios6 autorotation portraitupsidedown to portrait

查看:160
本文介绍了ios6 autorotation portraitupsidedown to portrait的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请不要建议我在ios 4.3-6.0的1版本中进行轮换是个坏主意,因为我告诉了很多次而且没有听我说。

Please don't suggest me it is a bad idea to have the rotation at ios 4.3-6.0 in 1 build, because I told to many times and didn't listen to me.

在项目设置中,我已经设置了所有支持的接口方向,只是为了确定。

At project settings I have setted all interface orientations to be supported, just to be sure.

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

Right now I am testing on ios6 with iphone4. Starting at app delegate it is a code, like this:

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个自定义类,在自动旋转的情况下推荐使用,对于它们问题已经解决了。

So I did 2 custom classes, which are recommended in may cases for autorotations, and for they the problem is solved.

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

The RotatingNavigationController is a custom UINavigationController has a bit ugly code, but I have collected this parts from this forum, so it should be allowed to post it:

@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

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

    just to be sure I have copy-pasted the code:

    // 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

    His job is to change the different UIViewController based on device state ( rotations )

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

    I have somewhere a Menu screen, which has different xib, but h and m file too ( because has different elements, which doesn't appear in Portait or Landscape. ( I don't have the power to change the whole architecture )

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

    A part of the code - and here should be some problem, I think is below in this 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];
            } 
    ...
    

    问题是:景观被取出当被颠倒的时候被推到了画像上,但那个画面没有旋转,它显示出一个破碎的布局。

    The problem is: the landscape is taken out and is pushed the portait, when is upside down, but that portait aren't rotating and it shows a broken layout.

    我怎么能唤醒那个控制器并告诉他是时候旋转,因为它不是纵向模式?

    How can I wake up that controller and tell him is time to rotate, because it isn't in portrait mode?

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

    Thanks any suggestion related to my question of any improvement beside of architecture change.

    更新:

    在app delgate没有帮助添加:

    at app delgate doesn't helped adding the:

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

    来自其他答案的代码:此处提供

    code from other answer: presented here

    Update2:

    UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation; 
    

    不想拿 UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown 由于某些原因的价值,我认为需要解决。

    doesn't want to take the UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown value for some reason, I think that need to be solved.

    Update3:
    非常小心地阅读两次,第三次 ios6发行说明


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

    The system determines whether an orientation is supported by intersecting the value returned by the app’s supportedInterfaceOrientationsForWindow: method with the value returned by the supportedInterfaceOrientations method of the top-most full-screen controller.




    • 现在再次阅读: )

    • 推荐答案

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

      Add this category to viewcontroller which doesn't rotate

      @implementation UINavigationController(Rotation)
      
      -(NSUInteger)supportedInterfaceOrientations {
          return UIInterfaceOrientationMaskAll;
      }
      
      @end
      

      这篇关于ios6 autorotation portraitupsidedown to portrait的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆