objective-c - 复杂的多设备方向处理

标签 objective-c xcode uiinterfaceorientation ios12

我有一个同时在 iPad 和 iPhone 上运行的通用应用程序。该应用程序以一个 .xib 文件开始,该文件内置于界面构建器中,充当启动图像。应用启动后,它会根据应用委托(delegate)中设置的设备尺寸切换到适当的 View Controller :

CGSize screenSize = [[UIScreen mainScreen] bounds].size;

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    if (screenSize.height <= 568.0f) {
        // iPhone 4, 4S, 5, 5C, 5S, SE
        self.viewController = [[iPhoneSmallViewController alloc] init];
    } else {
        // All other iPhones
        self.viewController = [[iPhoneLargeViewController alloc] init];
    }
} else {
    // All iPad models
    self.viewController = [[iPadViewController alloc] init];
}

iPad View Controller 支持所有界面方向(在应用程序目标/主设置页面中设置),但在 iPhone 上我只允许在 View Controller 中限制纵向模式:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}

这个方法有两个问题:

  1. 如果 iPhone 水平放置,应用程序仍会以纵向模式加载(根据限制,这一切都很好)但所有测量值在初始化时都是横向的。 UI 元素突出在侧面,因为它们是针对横向 View 测量的,但放置在纵向 View 上。

我通过在 ViewDidLoad 方法中初始化以下变量,使用窗口的大小来设置 View 内的所有内容:

windowSize = [[UIScreen mainScreen] bounds].size;

Tt 给我横向尺寸的手机是水平的,即使横向模式是不允许的。

  1. 如果应用最初以横向尺寸加载,我在应用委托(delegate)中对屏幕尺寸的所有排序都会关闭,因为我通过测量屏幕宽度来识别 iPhone 型号,而这仅适用于纵向模式。

问题:有没有人有办法以优雅和简单的方式处理这个复杂的问题?

一些附加信息:我使用 Xcode 10,一直支持 iOS9 并在 Objective C 中以编程方式执行所有操作。

p.s:我认为这种方法以前有效,但在 iOS 12 中不再有效。但我可能是错的...

编辑:我提供了我想要完成的图像,所有帮助将不胜感激。正如我所说,这以前有效(该应用程序很旧),但在最近的 iOS 版本中,错误越来越多,迫切需要清理,这正是我需要帮助的地方。

可能会解决我的问题的一件事是,如果我能以某种方式根据 launchScreen.xib 中的设备类型限制界面方向,因为我相信这就是导致 iPhone 出现错误行为的原因。

enter image description here

最佳答案

也许这个 SO 会有所帮助, 他们正在检测设备方向然后旋转 View ,看第一个答案:

Change orientation programmatically with button - iOS

UIInterfaceOrientation currentOrientation = [UIApplication         sharedApplication].statusBarOrientation;
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];    
[UIViewController attemptRotationToDeviceOrientation];

这里解释了一种更好的检测设备的方法: iOS detect if user is on an iPad

if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
    return YES; /* Device is iPad */
}

关于objective-c - 复杂的多设备方向处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55715202/

相关文章:

objective-c - 使用字典值动态填充对象属性

ios - 将 UITabBar 放置在 UITabBarController 中的屏幕顶部

ios - 构建阶段脚本未在我的 $PATH 中获取

ios - 如何仅针对横向禁用 UIPageViewController 的分页?

ios - 我可以通过电子邮件将 UIDocuments 作为附件发送吗?

ios - Objective C 新头文件标有灰色图标且无法导入框架

iOS - 通过我们的用户交互定期更新 UILabel 文本

ios - Xamarin 图像集不导入

objective-c - 在 iOS 6.0 中支持不同 View Controller 的不同方向

iphone - UITabBar 识别纵向或横向方向