簡體   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