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

时间:2012-10-03 22:46:20

标签: ios xcode ipad ios6 xcode4.5

我刚刚在XCode 4.5上打开了我的iPad项目。我的应用程序旨在以横向模式运行。它在以前版本的XCode上完美运行。但是在XCode 4.5上,它旋转了90°(四分之一圈),屏幕右侧有一个空白区域(我的视图大小正确但离开了屏幕)。它看起来像这样:

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

有人有这个问题吗?

任何帮助将不胜感激!

6 个答案:

答案 0 :(得分:6)

感谢大家的回复。我终于找到了解决方案。

首先,检查目标摘要菜单中的所有启动图像的方向和尺寸是否正确(项目的蓝色图标=> target => summary =>滚动到底部);如果尺寸或方向不正确,则会在启动图像上收到未正确设置的警告。

到目前为止,我有一个具有这种结构的项目(旧的XCode时尚):

  • AppDelegate.h和.m
  • RootViewController.h和.m
  • MainWindow-Iphone.xibMainWindow-iPad.xib(在Interface Builder中链接RootViewController;请参阅下面的屏幕截图,其中黄色/棕色图标(内部有正方形)相对于{{1 }})

下面是它的样子截图:

My old project structure

以下是我的AppDelegate的applicationDidFinishLaunching:方法中的代码:

RootViewController

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

  1. 我删除了- (void)applicationDidFinishLaunching:(UIApplication *)application { [window addSubview:[rootViewController view]]; [window makeKeyAndVisible]; } MainWindow.xib,现在以编程方式创建了我的窗口(更清晰,更好,以确保它符合任何屏幕的大小)
  2. 我删除了我的MainWindow-iPad.xib中定义的“主nib文件基本名称”和“主nib文件基本名称(iPad)”键(并设置为Info.plistMainWindow.xib
  3. 我添加了空MainWindow-iPad.xibRootViewController_iPhone.xib
  4. 我更改了RootViewController_iPad.xib方法中的代码:

    applicationDidFinishLaunching

    }

  5. 现在,一切正常! iPad上的方向是正确的!它比以前更加清晰:)我甚至不必改变已弃用的方法,如

    - (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];
    

    顺便说一下,为了确保所有视图都是全屏的(在iPhone 5上),请确保在Interface Builder中将视图设置为“缩放以填充”模式,并单击“自动调整子视图”。如果您的某些视图没有扩展到全屏,则可能是由于您创建控制器/视图的顺序(superView仅在创建它时(superView)向其子视图发送通知)。要轻松解决此问题,只需在- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    中添加以下代码即可
    - (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]; 确实会向子视图发送调整大小通知。

    希望这会有所帮助!

答案 1 :(得分:4)

确保您也在设置window.rootViewController。我遇到了同样的问题,但这条线为我修好了:

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

[[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)];

答案 4 :(得分:0)

如果您正在使用Storyboard,那么一个简单的方法就是使用loadView方法。它在viewDidLoad之前调用。只需转到故事板并删除关联的视图,因为您将在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;
}

答案 5 :(得分:0)

按顺序执行几个简单的步骤就可以解决这个问题。

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

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