ben*_*oy9 5 ios responsive-design swift responsive swiftui
我最初是为 iPad 设计的应用程序,现在也想为 iPhone 添加功能。有没有办法检查正在使用的设备是什么,然后相应地显示视图?
结构化英语:
IF current_device == iPhone THEN
DISPLAY iPhoneView
ELSE IF current_device == iPad THEN
DISPLAY iPadView
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我还希望 iPad 视图仅水平可用,然后 iPhone 视图仅在可能的情况下垂直可用。
Wit*_*ski 12
您正在寻找的是尺码类别。
\n要在 SwiftUI 视图中读取当前水平尺寸类别,您可以参考环境值horizontalSizeClass
@Environment(\\.horizontalSizeClass) var horizontalSizeClass\nRun Code Online (Sandbox Code Playgroud)\n然后在你的 SwiftUI 中像这样使用它View:
var body: some View {\n if horizontalSizeClass == .compact {\n return CompactView() // view laid out for smaller screens, like an iPhone\n } else {\n return RegularView() // view laid out for wide screens, like an iPad\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n值得注意的是,并非所有 iPhone 都是compact水平放置的,compact在多任务配置时,iPad 上也存在尺寸类别。您可以在设备尺寸类别和多任务尺寸类别下找到所有可能的组合。
一些可能有帮助的文章
\n\n| 归档时间: |
|
| 查看次数: |
2541 次 |
| 最近记录: |