How to set Flow layout for this scenario using UICollectionview?

夙愿已清 提交于 2019-12-13 07:29:40

问题


I Wanted to design the UICollectionview both portrait and landscape mode.Here i am using UICollectionViewFlowLayout

I have entered my view controller code

import UIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    @IBOutlet var collectionObject: UICollectionView!
    var isOrientation = true
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        layout.itemSize = CGSize(width: 90, height: 90)
        layout.scrollDirection = .horizontal
        collectionObject.collectionViewLayout = layout
        collectionObject!.register(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
        NotificationCenter.default.addObserver(self, selector: #selector(self.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
        collectionObject.delegate = self
        collectionObject.dataSource = self
        self .adjustContentInsets()
    }
    func rotated() {
        if UIDevice.current.orientation.isLandscape {
            print("Landscape")
            // layout.scrollDirection = .vertical
            isOrientation = false
        } else {
            print("Portrait")
            //layout.scrollDirection = .horizontal
            isOrientation = true

        }
        collectionObject?.reloadData()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //MARK: UICOLLECTIONVIEW DELEGATES AND DATASOURCE
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell  = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
        cell.backgroundColor = UIColor.orange
        cell.textLabel.text = String(indexPath.item)
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        var width :CGFloat = 0
        var height :CGFloat = 0
        if isOrientation == true {
            width = self.collectionObject!.frame.size.width / 2
            height = self.collectionObject!.frame.size.height / 2
        }else{
            width = self.collectionObject!.frame.size.height / 2
            height = self.collectionObject!.frame.size.height / 2
        }
        if indexPath.row == 0 {
            return CGSize(width: width, height: height)
        }else if(indexPath.row == 1 || indexPath.row == 2){
            return CGSize(width: width / 2, height: height / 2)
        }else{
            var isLwidth :CGFloat = 0
            if isOrientation == true {
                isLwidth = self.collectionObject!.frame.size.width / 2 - 40
            }else{
                isLwidth = self.collectionObject!.frame.size.width / 2 - 3
            }
            return CGSize(width: isLwidth, height: 100)
        }

        //return CGSize(width: self.view.frame.size.width-200, height: self.view.frame.size.height-250)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets{
         return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
    }
    func adjustContentInsets() {
        let inset = UIEdgeInsetsMake(UIApplication.shared.statusBarFrame.size.height, 0, 0, 0)
        self.collectionObject.contentInset = inset
        self.collectionObject.scrollIndicatorInsets = inset
    }


}

My output not coming as excepted:

Portrait Mode

Landscape Mode

The output is entire wrong, i am doing wrong with sizeForItemAt delegate.How to make layout like below using UICollectionview?

Actual Design: Portrait

Landscape

Here The collection view should not scroll both direction.The array count always 10 only. I wanted to fit CollectionViewCell with in view Bounds The bottom blue colour view is different UIView. Or Is there any possible way to achieve this by single control? Thanks in advance!!!

来源:https://stackoverflow.com/questions/41376290/how-to-set-flow-layout-for-this-scenario-using-uicollectionview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!