我有一款应用可以在iPhone和iPod Touch上运行,也可以在Retina iPad和其他设备上运行,但需要做一些调整。我需要检测当前设备是否是iPad。我可以用什么代码来检测用户是否在我的UIViewController中使用iPad,然后相应地改变一些东西?
当前回答
为什么这么复杂?我就是这么做的…
斯威夫特4:
var iPad : Bool {
return UIDevice.current.model.contains("iPad")
}
这样你就可以说if iPad {}
其他回答
在Swift中,您可以使用以下等式来确定通用应用程序上的设备类型:
UIDevice.current.userInterfaceIdiom == .phone
// or
UIDevice.current.userInterfaceIdiom == .pad
用法就像这样:
if UIDevice.current.userInterfaceIdiom == .pad {
// Available Idioms - .pad, .phone, .tv, .carPlay, .unspecified
// Implement your logic here
}
我认为这些答案都不能满足我的需要,除非我从根本上误解了某些东西。
我有一个应用程序(最初是一个iPad应用程序),我想在iPad和Mac上运行,在Catalyst下。我正在使用plist选项来缩放Mac界面以匹配iPad,但如果这是合理的,我想迁移到AppKit。当在Mac上运行时,我相信上述所有方法都告诉我,我是在iPad上运行。Catalyst的伪装相当彻底。
对于大多数问题,我确实理解代码在Mac上运行时应该假装它在iPad上。一个例外是,在Catalyst下滚动选择器在Mac上不可用,但在iPad上可用。我想弄清楚是否在运行时创建一个UIPickerView或做一些不同的事情。运行时选择是至关重要的,因为我希望在iPad和Mac上长期使用一个二进制文件,同时充分利用它们所支持的UI标准。
这些api可能会给普通的pre-Catalyst读者带来误导性的结果。例如:[UIDevice currentDevice]。当在Mac上的Catalyst下运行时,model返回@“iPad”。用户界面惯用的api维持了同样的错觉。
我发现你真的需要更深入地研究。我从以下信息开始:
NSString *const deviceModel = [UIDevice currentDevice].model;
NSProcessInfo *const processInfo = [[NSProcessInfo alloc] init];
const bool isIosAppOnMac = processInfo.iOSAppOnMac; // Note: this will be "no" under Catalyst
const bool isCatalystApp = processInfo.macCatalystApp;
然后,您可以将这些查询与[deviceModel hasPrefix: @"iPad"]这样的表达式结合起来,以整理出我所面临的各种微妙之处。对于我的例子,我明确地想要避免制作一个UIPickerView,如果指示的isCatalystApp为真,独立于“误导”信息的接口习语,或由isIosAppOnMac和deviceModel维持的幻觉。
现在我很好奇,如果我把Mac应用程序移动到我的iPad挎斗上会发生什么……
我发现在Xcode中的模拟器中,有些解决方案并不适合我。相反,这是可行的:
ObjC
NSString *deviceModel = (NSString*)[UIDevice currentDevice].model;
if ([[deviceModel substringWithRange:NSMakeRange(0, 4)] isEqualToString:@"iPad"]) {
DebugLog(@"iPad");
} else {
DebugLog(@"iPhone or iPod Touch");
}
斯威夫特
if UIDevice.current.model.hasPrefix("iPad") {
print("iPad")
} else {
print("iPhone or iPod Touch")
}
同样在Xcode中的“其他示例”中,设备模型返回为“iPad模拟器”,因此上述调整应该将其整理出来。
为什么这么复杂?我就是这么做的…
斯威夫特4:
var iPad : Bool {
return UIDevice.current.model.contains("iPad")
}
这样你就可以说if iPad {}
在Swift 4.2和Xcode 10中
if UIDevice().userInterfaceIdiom == .phone {
//This is iPhone
} else if UIDevice().userInterfaceIdiom == .pad {
//This is iPad
} else if UIDevice().userInterfaceIdiom == .tv {
//This is Apple TV
}
如果你想检测特定的设备
let screenHeight = UIScreen.main.bounds.size.height
if UIDevice().userInterfaceIdiom == .phone {
if (screenHeight >= 667) {
print("iPhone 6 and later")
} else if (screenHeight == 568) {
print("SE, 5C, 5S")
} else if(screenHeight<=480){
print("4S")
}
} else if UIDevice().userInterfaceIdiom == .pad {
//This is iPad
}
推荐文章
- 我应该如何从字符串中删除所有的前导空格?- - - - - -斯威夫特
- Xcode构建失败“架构x86_64未定义的符号”
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- registerForRemoteNotificationTypes: iOS 8.0及以上版本不支持
- 新的自动引用计数机制是如何工作的?
- 如何测试对象在Objective-C中的类?
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 从Cocoa应用程序执行一个终端命令
- Swift:理解// MARK
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- Swift编译器错误:“框架模块内的非模块化头”
- 从父iOS访问容器视图控制器