【问题标题】:Landscape orientation for Collection View in SwiftSwift 中集合视图的横向方向
【发布时间】:2016-05-11 02:24:51
【问题描述】:

我在收藏视图单元格的横向方向上遇到问题。当应用程序处于纵向时,它为我提供了每行正确的单元格数,即 2。但是当我将应用程序旋转为横向时,它每行显示 1 个单元格。这是我得到的屏幕:

纵向:

风景:

这是我添加单元格大小的代码:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

        var screenSize = CGFloat()
        if UIDevice.currentDevice().orientation.isLandscape.boolValue {
            screenSize = UIScreen.mainScreen().bounds.size.width
        }
        else {
            screenSize = UIScreen.mainScreen().bounds.size.width
        }

        let cellWidth = screenSize / 2.0

        return CGSizeMake(cellWidth, cellWidth)
    }

【问题讨论】:

    标签: ios xcode swift uicollectionview uicollectionviewcell


    【解决方案1】:

    你可以这样做

    let orientation = UIApplication.sharedApplication().statusBarOrientation
    if(orientation == .LandscapeLeft || orientation == .LandscapeRight)
    {
        return CGSizeMake((yourCollectionView.frame.size.width-10)/2, (yourCollectionView.frame.size.height-10)/2)
    }
    else{
        return CGSizeMake((yourCollectionView.frame.size.width-5)/2, (yourCollectionView.frame.size.height-10)/3)
    }
    

    此代码实现的效果:- 如果您的视图是纵向的,您将看到 2 列和 3 行,如果您的视图是横向的,您将看到 3 列和 2 行。您在代码中看到的推论是两个连续单元格之间的间距。因此,如果有 3 列,则扣除 15,如果有 2 列,则扣除 10,假设两个单元格之间的间距为 5。行也是如此。

    如果需要,您也可以使用屏幕大小,但我的集合视图中有自动布局约束以匹配屏幕大小,因此结果相同。我希望这就是你要找的。​​p>

    【讨论】:

    • 嗨,这是我正在寻找的最接近的内容,但我希望它与具有 2 列的纵向和横向相同。
    • 这很容易实现。我会为你更新我的答案。但我不认为它会很好看。
    【解决方案2】:

    只需重新加载您的UICollectionView

    覆盖

    func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
            super.viewWillTransition(to: size, with: coordinator)
    
            yourCollectionView.reloadData()
        }
    

    当方向改变时,您的集合视图布局将再次刷新

    更新: 这是collectionView 布局的代码 sn-p,在我的代码中的每一行中生成 4 个图像视图,即使它的方向从纵向变为横向,反之亦然。您可以根据需要更改它:

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.width / 4.8, height: UIScreen.main.bounds.width / 4.8)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    
        return UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
    }
    
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    
        return 0.0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout
        collectionViewLayout: UICollectionViewLayout,
                        minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    
        return 16.0
    }
    

    希望对您有所帮助。

    干杯!

    【讨论】:

      【解决方案3】:

      斯威夫特 5

      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
          var flag:CGSize? = nil
          if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad
          {
              if UIApplication.shared.statusBarOrientation.isPortrait{
      
                  let cellWidth = floor((collectionView.bounds.width - 5) / 2)
                  flag = CGSize(width: cellWidth, height: cellWidth)
              }
      
              else if UIApplication.shared.statusBarOrientation.isLandscape{
      
                  let cellwidth = floor((collectionView.bounds.width - 5) / 4)
                  flag =  CGSize(width: cellwidth, height: cellwidth)
      
              }
      
          }
          else if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone{
      
              if UIApplication.shared.statusBarOrientation.isLandscape {
                let cellWidth = floor((collectionView.bounds.width - 5) / 2)
                  flag = CGSize(width: cellWidth, height: cellWidth)
              } else if UIApplication.shared.statusBarOrientation.isPortrait{
                  //  let cellWidth = floor((collectionView.bounds.width - 5))
                  flag = CGSize(width: 402 , height: 185)
              }
      
          }
      
          return flag!
      }
      

      【讨论】:

      【解决方案4】:

      不要重新加载您的集合视图,只是使您的集合视图 flowlayour Done 无效

      如果您不明白这意味着什么,只需复制并粘贴此代码并更改您的集合视图名称

      覆盖 func viewWillLayoutSubviews() { super.viewWillLayoutSubviews()

      fiveCellCustomCollectionView 是我的自定义视图,customCollectionView 是集合视图

        guard let flowLayout = fiveCellCustomCollectionView.customCollectionView.collectionViewLayout as? UICollectionViewFlowLayout else { return }
        flowLayout.invalidateLayout()
      }
      

      【讨论】:

        【解决方案5】:

        Swift 4.2

        let orientation = UIApplication.shared.statusBarOrientation
            if(orientation == .landscapeLeft || orientation == .landscapeRight) {
                return CGSize(width: (collectionView.frame.size.width-10)/2, height: (collectionView.frame.size.height-10)/2)
            }
            else {
                return CGSize(width: (collectionView.frame.size.width-5)/2, height: (collectionView.frame.size.height-10)/3)
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-06-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多