11

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

视图(红色)旋转 90° 并在横向模式下离开屏幕

我检查了以下帖子但没有帮助:

ios6中的方向问题

ios6从横向到纵向模式的旋转问题

在 xcode 4.5 GM IOS 6 中将方向设置为横向模式

有人有这个问题吗?

任何帮助,将不胜感激 !

4

6 回答 6

6

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

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

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

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

下面是它的截图:

我的旧项目结构

这是我的 AppDelegate 的 applicationDidFinishLaunching: 方法中的代码:

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

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

}

我所做的是更接近您在使用 XCode 4.5 创建一个空项目时获得的结构。最后:

  1. 我删除了MainWindow.xibandMainWindow-iPad.xib现在以编程方式创建了我的窗口(更清晰,更好地确保它适合任何屏幕的大小)
  2. 我删除了在我的Info.plist(并设置为MainWindow.xiband MainWindow-iPad.xib)中定义的“Main nib file base name”和“Main nib file base name (iPad)”键
  3. 我添加了空RootViewController_iPhone.xibRootViewController_iPad.xib
  4. 我改变了我的applicationDidFinishLaunching方法中的代码,如下所示:

    - (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 中设置为“缩放以填充”模式并单击“自动调整子视图”。如果您的某些视图没有缩放到全屏,这可能是由于您创建控制器/视图的顺序(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确实向子视图发送调整大小通知。

希望这会有所帮助!

于 2012-10-09T23:51:32.777 回答
4

确保您也设置了 window.rootViewController。我有同样的问题,但这条线为我解决了这个问题:

MainViewController *mainView = [[MainViewController alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = mainView;
于 2012-10-08T21:31:33.920 回答
0

我在升级的应用程序上遇到了类似的问题。我还没有发现它记录在案,但似乎发生了变化。我最终发现新窗口似乎不知道旋转或大小,直到它成为关键和可见。我能够在 makeKeyAndVisible 之后立即将我的框架设置移动并且一切正常。我希望这对你有帮助。

于 2012-10-09T03:14:18.647 回答
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)];
于 2013-07-18T12:05:25.633 回答
0

如果您使用 Storyboards,一个简单的技巧是使用 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;
}
于 2013-09-12T18:13:58.880 回答
0

几个简单的步骤将为您解决这个问题。

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

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

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

 }

做这样的事情

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

 window.rootViewController = navigationController;
 [window makeKeyAndVisible];

 }

如果您将导航控制器设置为根视图控制器。

其次,如果您需要在您的 navigationController 的推送视图控制器中控制旋转方法,请为 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
于 2013-11-21T07:16:31.810 回答