在iOS中的Orientation上加载不同的xib文件 [英] Load different xib files on Orientation in iOS

查看:142
本文介绍了在iOS中的Orientation上加载不同的xib文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个 .xib 文件一个用于纵向模式,另一个用于横向模式,

I have created two .xib file one for portrait mode and other for landscape mode,

每个我希望加载相应的 .xib 文件,

on each rotation i want to load respective .xib file,

这是我的代码片段,

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 class

- (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];
    }

}

写完这么多代码后我的应用程序没有显示任何内容..它只显示黑屏。

After writing this much of code my app shows nothing.. it shows only black screen.

请提出一些解决方案。

please suggest some solution.

推荐答案

一旦尝试这样

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

将此方法添加到应用视图控制器以获取通知。

add this method into the app view controller for getting notification.

-(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];
    }

}

这篇关于在iOS中的Orientation上加载不同的xib文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆