XCode 4.5/iOS6:我的iPad应用程序在横向模式下的方向问题

XCode 4.5/iOS6:我的iPad应用程序在横向模式下的方向问题

问题描述:

我刚刚在XCode 4.5上打开了我的iPad项目。我的应用程序设计为在横向模式下运行。它可以在以前版本的XCode上完美工作。但在XCode 4.5上,它旋转了90度(四分之一转),屏幕右侧有一个空白区域(我的视图尺寸正确,但不在屏幕中)。它看起来像这样:XCode 4.5/iOS6:我的iPad应用程序在横向模式下的方向问题

The view (red) rotated of 90° and going of the screen in landscape mode

我选了以下职位,但并没有帮助:

orientation issue in ios6

ios6 Rotation issue from landscape to portrait mode

Set orientation to landscape mode in xcode 4.5 GM IOS 6

任何人有这个问题?

任何帮助,将不胜感激!

+0

检查支撑取向。 – endy

+0

在Xcode去iOS 5.1模拟器,并检查它是否正常工作,如果是的话,那么有一些代码需要完成,使应用程序与iOS6兼容,并请粘贴代码用于定位.... –

+0

是的应用程序在iOS 5.1 iPad模拟器上运行良好。 iOS 6 iPad模拟器出现问题(与设备上的旋转问题相同)。我认为我将从一个空白项目重新启动并转移我的代码并可能重新创建xib ...将会编写一些代码,因为用于管理旋转的前代码在iOS6中已弃用。我现在会让你...谢谢苹果所有这些疯狂的工作! –

谢谢大家的回复。我终于找到了解决方案。首先,检查所有启动图像在目标摘要菜单中是否有正确的方向和正确的大小(您的项目的蓝色图标=> target => summary =>在底部滚动);如果尺寸或方向不正确,则会在启动图像上发出警告,但设置不正确。

到现在为止,我有这个结构的项目(旧的XCode时尚):

  • AppDelegate.h和.M
  • RootViewController.h和.M
  • 一个MainWindow-Iphone.xibMainWindow-iPad.xib(与RootViewController链接在Interface Builder中;参见下面的屏幕截图,其中带有黄色/棕色图标(内部为正方形),相对于RootViewController

这里是什么样子的截图如下:

My old project structure

这里是什么在的applicationDidFinishLaunching代码:我的AppDelegate的方法:

- (void)applicationDidFinishLaunching:(UIApplication *)application { 

    [window addSubview:[rootViewController view]]; 
    [window makeKeyAndVisible]; 

} 

我所做的是要更接近您在使用XCode 4.5创建空项目时获得的结构。因此:

  1. 我删除了MainWindow.xibMainWindow-iPad.xib,现在创建了窗口编程(更清晰,更好地确保它适合任何屏幕大小)
  2. 我删除了“主笔尖文件基本名称”和这是在定义的“主笔尖文件基础名称总览”键我Info.plist(和设置为MainWindow.xibMainWindow-iPad.xib
  3. 我加入空RootViewController_iPhone.xibRootViewController_iPad.xib
  4. 我在改变了代码方法是这样的:

    - (void)applicationDidFinishLaunching:(UIApplication *)application {  
    
    NSLog(@"applicationDidFinishLaunching"); 
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
        self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPhone" bundle:nil]; 
    } else { 
        self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPad" bundle:nil]; 
    } 
    
    self.window.rootViewController = self.rootViewController; 
    
    [self.window makeKeyAndVisible]; 
    

    }

而现在,一切正常! iPad上的方向是正确的!而且它比以前:)我甚至没有改变像

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

过时方法对了更加清晰,以确保所有的意见将是全屏幕(在iPhone 5)做确保您的视图设置为Interface Builder中的“Scale to fill”模式,并单击“Autoresize subviews”。如果某些视图不能缩放为全屏,可能是由于创建控制器/视图的顺序(superView只在创建了superView时才向其子视图发送通知)。要轻松地解决这个问题,只需添加在- (void)viewDidLoad method下面的代码:

CGRect screenBounds = [[UIScreen mainScreen] bounds]; 
    [self.view setFrame:CGRectMake(0,0,screenBounds.size.width,screenBounds.size.height)]; 

或使用:代替

[self presentModalViewController:myViewController animated:TRUE]; 

[self.view.superview addSubview:myViewController.view]; 

presentModalViewController确实将改变大小的通知子视图。

希望这会有所帮助!

+2

谢谢!刚刚添加'self.window.rootViewController = rootViewController;'并且工作正常! –

+0

这对我来说也是足够的。它也摆脱了常见的错误“应用程序应该有一个根视图控制器...” – Totoro

+0

@SergeyKopanev - 这也适用于我。我只是将'self.window.rootViewController = viewController;'添加到applicationDidFinishLaunching的底部。谢谢 – jsherk

确保你也设置了window.rootViewController。我有同样的问题,但这条线固定它对我来说:

MainViewController *mainView = [[MainViewController alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
window.rootViewController = mainView; 

我有一个类似的问题,我升级的应用程序。我没有找到它的记录,但似乎有变化。我最终发现的是,新窗口似乎并不知道旋转或大小,直到它被制作为关键和可见之后。我能够在makeKeyAndVisible和所有工作后立即将我的框架设置移动。我希望这可以帮助你。

[[yourViewController view] setFrame:CGRectMake(0.0, 0.0, 1024.0, 768.0)]; 
[[yourViewController view] setTransform:CGAffineTransformTranslate(CGAffineTransformMakeRotation(0.5*M_PI),128.0,128.0)]; 

如果您使用故事板,一个简单的方法就是使用loadView方法。它在viewDidLoad之前调用。只需转到Storyboard并删除相关的视图,因为您要在loadView中以编程方式创建它。然后返回到视图控制器类并复制以下代码:

- (void)loadView 
{ 
    // You can adjust the view size and location here. If using full screen use the following code. If you have a tab bar for example and also want to account for the top default iPad bar, use: CGRectMake(0, 20, 1024, 699) instead. 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 748)]; 
    view.backgroundColor = [UIColor whiteColor]; 
    view.autoresizesSubviews = NO; 

    self.view = view; 
} 

按顺序执行一些简单步骤将为您解决此问题。

首先,在AppDelegate.m中,检查是否将您的rootViewController添加为子视图。也就是说,不是这个,

- (void)applicationDidFinishLaunching:(UIApplication *)application { 

[window addSubview:[navigationController view]]; 
[window makeKeyAndVisible]; 

} 

做这样

- (void)applicationDidFinishLaunching:(UIApplication *)application { 

window.rootViewController = navigationController; 
[window makeKeyAndVisible]; 

} 

的东西,如果你设置一个导航控制器为你的根视图控制器。

第二,如果你需要得到内你navigationController的旋转方法控制推viewControllers,创建UINavigationController的一个类别,象这样:

#import "UINavigationController+Rotation.h" 

@implementation UINavigationController (Rotation) 

- (BOOL)shouldAutorotate { 

    BOOL result = self.topViewController.shouldAutorotate; 

    return result; 
} 

- (NSUInteger)supportedInterfaceOrientations { 

    return self.topViewController.supportedInterfaceOrientations; 
} 

@end 

现在,iOS 6的向上

这两个方向的方法

- (BOOL)shouldAutorotate
- (NSUInteger)supportedInterfaceOrientations

将在您的课堂内被调用。

这是必要的,因为旧的旋转的方法,如

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

已经从iOS的6

弃用所以最后,你需要在你的视图控制器中实现这些;像这样:

-(BOOL)shouldAutorotate 
    { 
     return YES; 
    } 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskAll; 
} 

如果你想让你的视图控制器旋转和支持所有的方向。

其他方向口罩:

  1. UIInterfaceOrientationMaskPortrait
  2. UIInterfaceOrientationMaskLandscapeLeft
  3. UIInterfaceOrientationMaskLandscapeRight
  4. UIInterfaceOrientationMaskPortraitUpsideDown
  5. UIInterfaceOrientationMaskLandscape
  6. UIInterfaceOrientationMaskAllButUpsideDown在您的info.plist