UICollectionView不会居中

时间:2019-04-29 03:38:33

标签: ios swift

我的全屏UICollectionView包含单元格。我已经检查了宽度和高度,并且视图覆盖了整个屏幕。但是,启用分页后,视图为no longer centered。如何设置单元格或集合视图的中心,使其占据整个屏幕?

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
    }

}

1 个答案:

答案 0 :(得分:0)

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

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