swift - 如何将 CollectionView 单元格彼此相邻对齐?

标签 swift

我正在为我的应用程序创建一个个人资料页面屏幕。屏幕显示所有用户最近的帖子。我使用 Storyboard 在 UICollectionView 中创建两个 UICollectionViewCell,一个显示您的个人资料信息,另一个显示您的帖子。请参阅以下内容,了解我如何在 Storyboard中设计它:/image/6TzwK.png

当我运行该应用程序时,我得到以下结果:/image/XmvY3.jpg

但是,我希望单元格的放置方式如下所示:/image/nWQ14.jpg

如何强制单元格对齐,使其看起来如上图所示?谢谢!

最佳答案

找到问题的答案。为您的类实现 UICollectionViewDelegateFlowLayout 委托(delegate)。然后,将以下方法包含到类中:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let width = UIScreen.main.bounds.width // get the width of the screen
    let scale = (width / 3) // get the width (and height) of each cell

    if indexPath.row != 0 { // check to see if the cell is the profile header
        return CGSize(width: scale, height: scale) // if not, then return the cell size
    }

    return CGSize(width: width, height: ((238/414) * width)) // if it is the profile header, return the size for it.
}

确保 UICollectionView Storyboard设置的“最小间距”为 0,0。然后,单元格将对齐到 GridView 中。

关于swift - 如何将 CollectionView 单元格彼此相邻对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50997350/

相关文章:

swift - 如何通过设置 frame 属性来移动或调整 NSView 的大小?

swift - 带有 Void 的通用类型别名在 Swift 4 中给出了缺少参数的错误

swift - 在收集单元格中滑动手势

swift - 在 Swift 框架中公开响应式(Reactive)可观察量

swift - 如何在 SLComposeViewController 中忽略 "Import with ' xxx' "

ios - swift POST 请求不向服务器发送数据

swift - macOS : programmatic check if process runs as a launchDaemon or launchAgent or from command-line

swift - 在 Swift 中循环播放 A 到 B 的动画

ios - 使用json以快速登录形式从mysql获取登录数据

ios - 在 UITableViewCell 中显示每个字典值