简体   繁体   English

Swift 中集合视图的横向方向

[英]Landscape orientation for Collection View in Swift

I am encountering a problem on landscape orientation for my collection view cell.我在我的集​​合视图单元格中遇到横向问题。 When the app is in portrait it gives me the correct number of cell per row which is 2. But when I rotate my app to landscape it display 1 cell per row.当应用程序处于纵向时,它为我提供了正确的每行单元格数,即 2。但是当我将应用程序旋转为横向时,它每行显示 1 个单元格。 Here's the screen of what I got:这是我得到的屏幕:

Portrait:肖像: 在此处输入图片说明

Landscape:景观: 在此处输入图片说明

Here's my code for adding the size of the cell:这是我添加单元格大小的代码:

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)
    }

Here is what you can do这是你可以做的

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)
}

What this code achieves:- If your view is in portrait you will see 2 column and 3 row and if your view is in landscape you will see 3 columns and 2 row.这段代码实现了什么:- 如果您的视图是纵向的,您将看到 2 列和 3 行,如果您的视图是横向的,您将看到 3 列和 2 行。 The deduction you see in the code is the spacing between two consecutive cells.您在代码中看到的扣除是两个连续单元格之间的间距。 So if there is 3 column the deduction is 15 and if there is 2 column the deduction is 10 assuming that the spacing between two cell is 5. Same goes for the row.因此,如果有 3 列,则扣除额为 15,如果有 2 列,假设两个单元格之间的间距为 5,则扣除额为 10。行也是如此。

You can use the screen size as well if you want but I had auto layout constraints in my collection view to match the size of the screen so it resulted the same.如果需要,您也可以使用屏幕大小,但我在我的集​​合视图中有自动布局约束以匹配屏幕大小,因此结果相同。 I hope this is what you were looking for.我希望这就是你要找的。

Just reload your UICollectionView in只需重新加载您的UICollectionView

override覆盖

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

        yourCollectionView.reloadData()
    }

and your collection view layout will refresh again when orientation changed当方向改变时,您的集合视图布局将再次刷新

Update: This is code snippet for collectionView layout making 4 image view in each row from my code, and its working even it changing orientation from portrait to landscape and vice versa.更新:这是collectionView布局的代码片段,从我的代码中的每行生成 4 个图像视图,它甚至可以将方向从纵向更改为横向,反之亦然。 You can change it to your need:您可以根据需要更改它:

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
}

Hope it will helps you.希望它会帮助你。

Cheers!干杯!

Swift 5斯威夫特 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!
}

Swift 4.2斯威夫特 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)
    }

don't reload your collection view just invalidate your collection view flowlayour Done不要重新加载您的集合视图,只是使您的集合视图无效。

if you don't understand what is that means just copy and paste this code and change your collection view name如果您不明白这意味着什么,请复制并粘贴此代码并更改您的集合视图名称

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

fiveCellCustomCollectionView is my custom view and customCollectionView is collection view FiveCellCustomCollectionView 是我的自定义视图,customCollectionView 是集合视图

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

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

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