Swift 中集合视图的横向方向 [英] Landscape orientation for Collection View in Swift

查看:15
本文介绍了Swift 中集合视图的横向方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

纵向:

风景:

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

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) ->CGS 大小 {var screenSize = CGFloat()如果 UIDevice.currentDevice().orientation.isLandscape.boolValue {screenSize = UIScreen.mainScreen().bounds.size.width}别的 {screenSize = UIScreen.mainScreen().bounds.size.width}让 cellWidth = screenSize/2.0返回 CGSizeMake(单元格宽度,单元格宽度)}

解决方案

你可以这样做

让方向= UIApplication.sharedApplication().statusBarOrientation如果(方向 == .LandscapeLeft || 方向 == .LandscapeRight){return CGSizeMake((yourCollectionView.frame.size.width-10)/2, (yourCollectionView.frame.size.height-10)/2)}别的{return CGSizeMake((yourCollectionView.frame.size.width-5)/2, (yourCollectionView.frame.size.height-10)/3)}

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

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

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. 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. 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.

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.

这篇关于Swift 中集合视图的横向方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆