如何切换方向变化的观点

时间:2012-12-07 23:54:57

标签: iphone ios ipad optimization screen-orientation

所以我目前正在创建一个包含六个视图的应用程序,每个视图专用于设备类型方向的组合。如果您不明白我的意思,我会标记所有六种组合:

  • iPhone4_(320x480)&肖像
  • iPhone4_(320x480)&景观
  • iPhone5_(320x568)&肖像
  • iPhone5_(320x568)&景观
  • iPad_(768x1024)&肖像
  • iPad_(768x1024)&景观

我知道解决所有这些麻烦似乎有点傻,但在这种情况下有点需要。无论如何,我一直试图尝试将Programming Guide的Apple解决方案与TheAppCodeBlog上的在线教程中给出的方法结合起来。

ViewController.m - > ViewDidLoad方法

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

ViewController.m - > orientationChanged方法

- (void) orientationChanged:(NSNotification *)object
{
    UIDeviceOrientation deviceOrientation = [[object object] orientation];
    if (deviceOrientation == UIInterfaceOrientationPortrait && [UIScreen mainScreen].bounds.size.height == 480.0)
    {
        self.view = self.portrait4View;
    }
    else if ((deviceOrientation == UIInterfaceOrientationLandscapeLeft) || (deviceOrientation == UIInterfaceOrientationLandscapeRight)) && [UIScreen mainScreen].bounds.size.height == 480.0)
    {
        self.view = self.landscape4View;
    }
    else if (deviceOrientation == UIInterfaceOrientationPortrait && [UIScreen mainScreen].bounds.size.height == 568.0)
    {
        self.view = self.portrait5View;
    }
    else if ((deviceOrientation == UIInterfaceOrientationLandscapeLeft) || (deviceOrientation == UIInterfaceOrientationLandscapeRight)) && [UIScreen mainScreen].bounds.size.height == 568.0)
    {
        self.view = self.landscape5View;
    }
    else if (deviceOrientation == UIInterfaceOrientationPortrait && [UIScreen mainScreen].bounds.size.height == 1024.0)
    {
        self.view = self.portraitPadView;
    }
    else
    {
        self.view = self.landscapePadView;
    }

}

每个视图的Interface Builder设置 Interface Builder

我也收到Expected identifier的一些错误: Mini Window

有些人可以帮助我,或者至少指出我正确的方向吗?

更新1

每当我启动应用程序时,我都会看到黑屏。这是我AppDelegate.m的样子:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_Portrait5" bundle:nil];
    }

    else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 480.0) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_Portrait4" bundle:nil];
    }

    else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_PortraitPad" bundle:nil];
    }
}

1 个答案:

答案 0 :(得分:1)

您与Expected Identifier相关的错误是因为您的父母错了。你的if条件应该是(我在其他几个条件中也会看到相同的错误):

else if (((deviceOrientation == UIInterfaceOrientationLandscapeLeft) || (deviceOrientation == UIInterfaceOrientationLandscapeRight)) && [UIScreen mainScreen].bounds.size.height == 480.0)