iphone - 想要针对不同的 iPhone 界面方向使用多个 Nib

标签 iphone ipad orientation

我遇到一种情况,我创建了两个不同的 Nib ,一个处于纵向模式,另一个处于横向模式。我的 View 中有很多设计,所以我不得不选择两种不同的 Nib 。现在,我想在界面旋转时切换 Nib

common viewController

这样我就可以保留 View 中的填充值和控件的状态。

现在我正在使用

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Override to allow orientations other than the default portrait orientation.
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight ){
    [self initWithNibName:@"LandscapeNib" bundle:nil];
}else if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
    [self initWithNibName:@"PortraitNib" bundle:nil];

}


return YES;

}

但它不会改变 Nib ,它显示初始加载的 Nib 。我猜它正在加载但没有显示,因为初始 Nib 已经显示,没有被删除。我无法找到使用通用 View Controller 使用多个 Nib 的解决方案,以便我可以轻松处理控件的功能?

最佳答案

您应该为横向模式创建一个特殊的 View Controller ,并在设备旋转时以模态方式呈现它。要在设备旋转时收到通知,请注册 UIDeviceOrientationDidChangeNotification 通知。

对于需要以纵向和横向不同方式呈现的复杂 View ,这是 Apple 推荐的实现方式。阅读此处了解更多详细信息:

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html#//apple_ref/doc/uid/TP40007457-CH101-SW26

这是 Apple 文档的摘录。在 init 或 awakeFromNib 中注册通知:

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

并在orientationChanged中以模态方式呈现您的横向 View Controller ,或将其关闭,所有这些都根据当前旋转:

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
        !isShowingLandscapeView)
    {
        [self presentModalViewController:self.landscapeViewController
                                animated:YES];
        isShowingLandscapeView = YES;
    }
    else if (deviceOrientation == UIDeviceOrientationPortrait &&
             isShowingLandscapeView)
    {
        [self dismissModalViewControllerAnimated:YES];
        isShowingLandscapeView = NO;
    }
}

关于iphone - 想要针对不同的 iPhone 界面方向使用多个 Nib ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3786727/

相关文章:

ios - 子 viewController 没有收到 shouldAutorotate 方法调用

iphone - 3D CAD 到 OpenGL

iphone - 随着音乐节拍闪烁的手电筒

javascript - HTML5 音频对象不在 iPad 上播放(当从 setTimeout 调用时)

java - 自动图像从纵向旋转为横向

iphone - 如何以编程方式检查 iPhone 上安装了哪些国际键盘?

iphone - "__workq_kernreturn"在 iOS 崩溃日志中或当您暂停应用程序执行时表示什么?

CSS 修复了背景缩放/平移在 iPad 上的效果不佳

android - 如何处理显示 ProgressDialog 的方向变化?

ios - 根据设备方向更改绘制矩形输出