我创建了两个 .xib
一个用于纵向模式,另一个用于横向模式,
在每次旋转时,我想分别加载 .xib
文件,
这是我的代码片段,
ViewAppDelegate.m 类
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if ([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait | [UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortraitUpsideDown)
{
self.viewController = [[OrientationViewController alloc] initWithNibName:@"PortraitController" bundle:nil];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navigationController;
}
else{
self.viewController = [[OrientationViewController alloc] initWithNibName:@"LandscapeController" bundle:nil];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navigationController;
}
[self.window makeKeyAndVisible];
return YES;
}
ViewController.m 类
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return YES;
}
-(void)viewWillLayoutSubviews{
if ([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait | [UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortraitUpsideDown)
{
[[NSBundle mainBundle] loadNibNamed:@"PortraitController" owner:self options:nil];
}
else{
[[NSBundle mainBundle] loadNibNamed:@"LandscapeController" owner:self options:nil];
}
}
写了这么多代码后,我的应用程序什么也没显示……它只显示黑屏。
请提出一些解决方案。
最佳答案
一旦这样尝试
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeOrientation) name:UIDeviceOrientationDidChangeNotification object:nil];
将此方法添加到应用程序 View Controller 中以获取通知。
-(void)changeOrientation {
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
[[NSBundle mainBundle] loadNibNamed:@"Portrait" owner:self options:nil];
}
else {
[[NSBundle mainBundle] loadNibNamed:@"Landscape" owner:self options:nil];
}
}
关于iphone - 在 iOS 中的 Orientation 上加载不同的 xib 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15540771/