ios - UICollectionView 不会居中

标签 ios swift

我有一个带有单元格的全屏 UICollectionView。我检查了宽度和高度, View 覆盖了整个屏幕。但是,当我启用分页时, View 是 no longer centered .如何设置单元格或 Collection View 的中心,使其占据整个屏幕?

import UIKit

class ShowPictures: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    var PICTURES: [Picture] = []

    @IBAction func swipeAway(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

    private func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return UIScreen.main.bounds.size
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return self.PICTURES.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let item: Messages.MessageAdapter.Item.Picture = self.PICTURES[indexPath.section]
        let cell: Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! Cell

        return cell
    }

    @IBAction func CloseController(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }

}

class Cell: UICollectionViewCell, UIScrollViewDelegate {

    @IBOutlet weak var _image: UIImageView!
    @IBOutlet weak var _scroll: UIScrollView!

    override func layoutSubviews() {
        self._scroll.delegate = self
        self._scroll.zoomScale = CGFloat(integerLiteral: 1)
    }

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return self._image
    }

}

最佳答案

添加以下委托(delegate)方法以删除插图:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return .zero
}

关于ios - UICollectionView 不会居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55896652/

相关文章:

iOS Facebook SDK 使用 Graph search 搜索用户

ios - 重复符号 FB Audience Network iOS

swift - 从 swift 的名称中获取属性的类型/类

swift - 在 iOS16.4 中 TextField 内的电子邮件提示呈现蓝色

ios - 在 xcode 中配置脚本失败,退出代码为 1

iphone - 将 UIWebView 呈现为图像/PDF 具有视觉伪像

ios - 在自定义 UITableviewCell 中设置图像不会*粘*

ios - Swift - 自定义颜色主题和 SFSafariViewController

ios - 如何按步数获取CMPedometer数据?

Swift "where"关键字