我想要两个独立的 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,但一切都没有到位,状态栏也没有旋转!