iphone - 在 iOS 中的 Orientation 上加载不同的 xib 文件

标签 iphone ios ipad orientation xib

我创建了两个 .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/

相关文章:

ios - 如何编写可以在 Objective C 中使用的带有 throws 语句和返回值的 swift 3 方法?

iphone - 将 iPhone 应用程序更新为通用应用程序

iphone - Windows 下 iOS 开发的替代方案

ios - iPhone 屏幕键盘的高度是多少?

iphone - 如何动态设置 UIlabel TextColor

ios - Flutter ios 设备不会从 FCM 通知触发 onMessage。实现 APN 时 Sendtodevice 失败

iphone - 如何在 IndexPath 处使用 reloadRows 更新行

iphone - 改变 iPhone 方向,然后再改回来,我的视野会减半吗?

iphone - 我需要提交 CCATS 吗?

ios - Swift:使用AVCaptureVideoDataOutput视频录制时如何将动态文本添加到CMSampleBuffer?