带滚动视图的ViewController显示空屏

时间:2016-09-13 10:26:21

标签: ios swift uiscrollview

我尝试使用scrollView创建一个视图控制器,里面有collectionView。我添加了scrollView,然后是collectionView,然后是另一个内容的容器视图,但不知何故,屏幕是空的。

代码正在关注

import UIKit

class BrowseProfileViewController:BaseViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

var photosArray:[UIImage]?
var cellId = "CellId"

var scrollView:UIScrollView = {
    var scrollView = UIScrollView()
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    return scrollView
}()

lazy var photoGalleryCollectionView:UICollectionView = {
    var layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .Horizontal
    layout.itemSize = CGSize(width: self.view.frame.width, height: self.view.frame.width)
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    layout.minimumLineSpacing = 0
    layout.minimumLineSpacing = 0
    var collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.translatesAutoresizingMaskIntoConstraints = false
    collectionView.pagingEnabled = true
    return collectionView
}()


var containerView:UIView = {
    var view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.setNavigationBarHidden(true, animated: false)

    photoGalleryCollectionView.registerClass(AlbumsCell.self, forCellWithReuseIdentifier: cellId)

    photosArray = [UIImage(named:"q")!, UIImage(named:"qq")!, UIImage(named:"qqq")!]

    view.addSubview(scrollView)
    scrollView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true
    scrollView.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
    scrollView.topAnchor.constraintEqualToAnchor(view.topAnchor).active = true
    scrollView.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true

    scrollView.addSubview(photoGalleryCollectionView)
    photoGalleryCollectionView.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
    photoGalleryCollectionView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true
    photoGalleryCollectionView.topAnchor.constraintEqualToAnchor(scrollView.topAnchor).active = true
    photoGalleryCollectionView.heightAnchor.constraintEqualToConstant(self.view.frame.width).active = true

    scrollView.addSubview(containerView)
    containerView.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
    containerView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true
    containerView.topAnchor.constraintEqualToAnchor(photoGalleryCollectionView.bottomAnchor).active = true
    containerView.bottomAnchor.constraintEqualToAnchor(scrollView.bottomAnchor).active = true

}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return photosArray!.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath) as! AlbumsCell
    cell.imageView.image = photosArray![indexPath.item]
    return cell

}

}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

未经测试代码,请检查contentSize的{​​{1}},可能是scrollViewCGSizeZero会尽可能拥抱它的内容,并且由于其子视图都没有UIScrollView,因此会将其收缩到explicit size。< / p>

有关如何将CGSizeZeroUIScrollView一起使用,请参阅ScrollViews上的Apples Documentation原子对象 BlogPost