[英]UICollectionView horizantal scrolling Photo Gallery with swift
我正在嘗試使用Swift的UICollectionView
制作照片庫。 我想以全屏顯示照片並向右滑動以進入下一張圖像。 我有一個縱向框架的工作代碼,但是一旦我去景觀,我的單元格似乎沒有重新調整到正確的位置和大小。
這意味着它始終保持縱向屏幕的width
, height
, x position
和y position
相同。 我也在縱向和橫向框架上都出現此錯誤:
- >未定義UICollectionViewFlowLayout的行為,因為:
- >項目高度必須小於UICollectionView的高度減去該部分的頂部和底部值。
我已經在網上尋找答案,並嘗試了所有可能的解決方案,但它似乎並不適合我。 讓我知道是否有人可以幫助解決這個問題。 非常感激。
這是我的代碼:
import UIKit
class ImageDetailViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UINavigationControllerDelegate {
@IBOutlet weak var imgDetailCollectionView: UICollectionView!
var index: Int!
var imageItems: Array<Items> = []
var currentIndex: CGFloat!
var size = UIScreen.mainScreen().bounds.size
init(index: Int , imageItems: Array<Items>) {
self.index = index
self.imageItems = imageItems
super.init(nibName: "ImageDetailViewController", bundle: NSBundle.mainBundle())
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
imgDetailCollectionView.registerNib(UINib(nibName: "ImageDetailCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ImageDetailCollectionViewCell")
//self.automaticallyAdjustsScrollViewInsets = false
imgDetailCollectionView.setZoomScale(0.5, animated: true)
}
override func viewDidLayoutSubviews() {
imgDetailCollectionView.scrollRectToVisible(CGRectMake(320 * CGFloat(index), 0, 320 , 240), animated: false)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return self.imageItems.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
var cell = imgDetailCollectionView.dequeueReusableCellWithReuseIdentifier("ImageDetailCollectionViewCell", forIndexPath: indexPath) as ImageDetailCollectionViewCell
cell.setup(imageItems[indexPath.row].url!)
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)){
self.imgDetailCollectionView.collectionViewLayout.invalidateLayout()
var frame = cell.frame
frame.size = CGSizeMake(size.width, size.height)
cell.frame = frame
}else{
self.imgDetailCollectionView.collectionViewLayout.invalidateLayout()
var frame = cell.frame
frame.size = CGSizeMake(size.height, size.width)
cell.frame = frame
}
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var size = UIScreen.mainScreen().bounds.size
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)){
return CGSizeMake(size.width, size.height)
}else{
return CGSizeMake(size.height, size.width)
}
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
self.imgDetailCollectionView.collectionViewLayout.invalidateLayout()
var currentOffset = self.imgDetailCollectionView.contentOffset
self.currentIndex = currentOffset.x / imgDetailCollectionView.frame.size.width
}
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
var currentSize = imgDetailCollectionView.bounds.size
var offset = self.currentIndex * currentSize.width
imgDetailCollectionView.setContentOffset(CGPointMake(offset, 0), animated: false)
imgDetailCollectionView.reloadData()
}
override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
self.imgDetailCollectionView.collectionViewLayout.invalidateLayout()
}
}
您的問題是在橫向模式下檢查方向並使用高度作為寬度。 這是不正確的。 旋轉設備時,width屬性將返回設備處於縱向時的實際高度。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = imgDetailCollectionView.dequeueReusableCellWithReuseIdentifier("ImageDetailCollectionViewCell", forIndexPath: indexPath) as ImageDetailCollectionViewCell
cell.setup(imageItems[indexPath.row].url!)
cell.frame.size = view.frame.size
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(view.frame.width, view.frame.height)
}
另外需要注意的是,apple建議您使用viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
方法,因為這是較新的API。 此外,在iOS9中,如果您使用此方法,則將使用此方法啟用多任務處理功能。
由於下一行,可能會打印此錯誤:
var size = UIScreen.mainScreen()。bounds.size
為了快速檢查,您可以輸入例如:
var size = UIScreen.mainScreen()。bounds.size * 0.25
如果錯誤消息消失,您應該按照消息中的說明從可用高度(collectionView.bounds.height)中減去collectionView的contentInsets和sectionInsets。
這樣做,您將避免單元格高度試圖比它的collectionView“更高”的情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.