4

스위프트와 함께 UICollectionView을 사용하여 포토 갤러리를 만들려고합니다. 전체 화면으로 사진을 표시하고 오른쪽으로 스 와이프하여 다음 이미지로 이동하려고합니다. 세로 프레임에 대한 작업 코드가 있지만 가로로 가면 마자 셀이 올바른 위치와 크기로 다시 조정되지 않는 것처럼 보입니다.UICollectionView 신속한 사진 갤러리

항상 세로 화면의 width, height,및 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() 
} 
} 

답변

0

귀하의 문제는 당신이 방향을 확인하고 가로 모드에 폭에 대한 높이를 사용하고 있습니다 :

여기 내 코드입니다. 이것은 잘못되었습니다. 장치를 회전 시키면 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) 
} 

또한 다른 노트, 애플이 새로운 API이기 때문에 당신이 viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) 방법을 사용하는 것이 좋습니다. 또한 iOS9에서이 방법을 대신 사용하면이 방법을 사용하여 멀티 태스킹 기능을 사용할 수있게됩니다.

0
이 오류 때문에 다음 라인의 인쇄 할 수

:.

VAR 크기 = UIScreen.mainScreen() 빨리 들어

당신이 예를 들어 입력 할 수 있습니다 확인 bounds.size :

VAR 크기 = UIScreen.mainScreen(). bounds.size * 0.25

오류 메시지가 나타나지 않으면 message의 지침을 따라 collectionView의 contentInsets 및 sectionInsets를 사용 가능한 높이 (collectionView.bounds.height)에서 뺍니다.

이렇게하면 셀 높이가 collectionView보다 '상위'인 상황을 피할 수 있습니다.