1

1 つは縦向き用、もう 1 つは横向き用の 2 つの別個の 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 をロードしますが、すべてが正しく配置されておらず、ステータス バーが回転していません!

4

0 に答える 0