如何将不同的XIB用于纵向/横向?

时间:2012-10-28 14:17:15

标签: rotation ios6 xib landscape

我想要两个独立的XIB,一个用于肖像,一个用于横向。

我的AppDelegate看起来:

#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIDevice *device = [UIDevice currentDevice];                    //Get the device object
    [device beginGeneratingDeviceOrientationNotifications];         //Tell it to start monitoring the accelerometer for orientation
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];    //Get the notification centre for the app
    [nc addObserver:self                                            //Add yourself as an observer
           selector:@selector(orientationChanged:)
               name:UIDeviceOrientationDidChangeNotification
             object:device];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
/*

    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
    } else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
    }
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
 */
    return YES;
}

- (void)orientationChanged:(NSNotification *)note
{
    UIDeviceOrientation deviceOrientation = [[note object] orientation];
    if(deviceOrientation ==UIInterfaceOrientationPortrait){
            NSLog(@"Changed Orientation To Portrait");
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
        } else {
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
        }
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
    }
    if(deviceOrientation ==UIInterfaceOrientationLandscapeLeft || deviceOrientation ==UIInterfaceOrientationLandscapeRight){
        NSLog(@"Changed Orientation To Landscape");
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            self.viewController = [[ViewController alloc] initWithNibName:@"landscape_iphone" bundle:nil];
        } else {
            self.viewController = [[ViewController alloc] initWithNibName:@"landscape_ipad" bundle:nil];
        }
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];

    }
}

@end

此代码加载另一个XIB,但一切都不在位,状态栏不会旋转!

0 个答案:

没有答案