繁体   English   中英

如何在 iPhone 的 list(tableView) 和 iPad 的 grid(collectionView) 上显示数据

[英]How to display the data on list(tableView) for iPhone and grid(collectionView) for iPad

我有一个代码评估,从来没有想过这样的事情; 我必须以两种不同的布局显示从 API 检索的数据:

  • 适用于 iPad; 仅在横向模式和网格中。 我会用 UIcollectionView 来做

  • 对于 iPhone; 仅在纵向模式和列表中。 我会用 UItableView 或 StackView 来做。

问题是如何实现从视图控制器填充集合视图或表视图的逻辑。 我想我必须使用 UITraitCollection 但我知道它是否是更好的选择。

有人可以帮助我吗? 谢谢

您可以通过此方法获取设备

// 1. 请求一个 UITraitCollection 实例

  let deviceIdiom = UIScreen.main.traitCollection.userInterfaceIdiom

// 2. 检查习语

  switch (deviceIdiom) {

 case .pad:
    print("iPad style UI")
 case .phone:
    print("iPhone and iPod touch style UI")
 case .tv: 
   print("tvOS style UI")
 default:
    print("Unspecified UI idiom")
}

并且对于方向使用此方法:-

struct Orientation {
// indicate current device is in the LandScape orientation
  static var isLandscape: Bool {
    get {
        return UIDevice.current.orientation.isValidInterfaceOrientation
            ? UIDevice.current.orientation.isLandscape
            : UIApplication.shared.statusBarOrientation.isLandscape
    }
  }
// indicate current device is in the Portrait orientation
  static var isPortrait: Bool {
    get {
        return UIDevice.current.orientation.isValidInterfaceOrientation
            ? UIDevice.current.orientation.isPortrait
            : UIApplication.shared.statusBarOrientation.isPortrait
      }
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM