ios6 autorotation portraitupsidedown 到 portrait

标签 ios uiviewcontroller ios4 autorotate

请不要建议我在 1 个版本中进行 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 文件(因为有不同的元素,不会出现在肖像或风景中。(我没有能力改变整个架构)

代码的一部分 - 这里应该有一些问题,我认为在这个 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];
        } 
...

问题是:风景被取出并被​​推到肖像上,当颠倒时,但那个肖像没有旋转并且显示出一个破损的布局。

我怎样才能唤醒那个 Controller 并告诉他是时候旋转了,因为它没有处于纵向模式?

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

更新:

在 app delgate 没有帮助添加:

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

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

更新2:

UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation; 

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

更新3: 仔细阅读两次和第三次 ios6 release notes

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

@implementation UINavigationController(Rotation)

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

@end

关于ios6 autorotation portraitupsidedown 到 portrait,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13704218/

相关文章:

ios - 在导航 View Controller 登录屏幕上运行 performSegueWithIdentifier 通过两个屏幕转换

ios - 如何使用完成处理程序正确关闭模态视图 Controller

iphone - 如何在调整 UIView 大小时设置动画

iphone:无法连接 socket ?

ios - 为什么我没有在 Xcode 源代码控制中获得 master 分支来快速在 github 中第一次上传项目

iphone - UIScrollView 缩放后如何重置?

ios - 如何使用解析添加两个PFquery数据的对象?

ios - 尝试用 ViewController 做一个简单的分数虽然难倒

ios swift 3 - UIAlertController 宽度

iphone - UIPickerView - 点击选择