ios - 设置列数和固定行数

标签 ios swift screen-orientation

在我的项目中,我有一个 collectionview,单元格之间的间距为 10px,行与行之间的间距也为 10px,collectionview 上方有一个 50px 的高 View ,下方有相同的 View (50px),所有元素之间有 8px 的间隔那个环绕。

我正在尝试,当 ipad 位置为横向时它有 4 列和 3 行,当位置为纵向时它有 3 列和 4 行。

capture image

这是我的代码

import Foundation
import UIKit

class VC_catalogo_modelos: UIViewController {


    @IBOutlet weak var collectionViewModelos: UICollectionView!

    var arrayModelos = ["1","2","3","4","5","6","7","8","9","10","11","12"]
    var espacioCollectionAncho = 0.0
    var espacioCollectionAlto = 0.0


    override func viewDidLoad() {
        super.viewDidLoad()

        //asigno el gesto que muestra el menu lateral
        if revealViewController() != nil {
            self.view.addGestureRecognizer(revealViewController().panGestureRecognizer())
            self.collectionViewModelos.addGestureRecognizer(revealViewController().panGestureRecognizer())
        }
        NotificationCenter.default.addObserver(self, selector: #selector(deviceRotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
        recalcularSizeCeldas()
        collectionViewModelos.reloadData()

    }
    @IBAction func botonVolverFamilias(_ sender: Any) {
        //instancio el viewcontroller de familias
        let sw  = revealViewController()
        self.view.window?.rootViewController = sw
        let viewControllerModelos = storyboard!.instantiateViewController(withIdentifier: "vc_catalogo_familias") as! VC_catalogo
        //asigno la vista del menu lateral y presento el viewcontroller
        let navigationController = UINavigationController(rootViewController: viewControllerModelos)
        navigationController.navigationBar.isHidden=false
        navigationController.setNavigationBarHidden(true, animated: false)
        sw!.setFront(navigationController, animated: true)
    }

    //calculo tamaño de la celda de modelo
    func recalcularSizeCeldas(){
        //HORIZONTAL
        if UIDevice.current.orientation.isLandscape {
            let width = (view.frame.size.width - 46) / 4
            let height = (view.frame.size.height - 156) / 3
            let layout = collectionViewModelos.collectionViewLayout as! UICollectionViewFlowLayout
            layout.itemSize = CGSize(width: width, height: height)
        }
        //VERTICAL
        else {
            let width = (view.frame.size.width - 36) / 3
            let height = (view.frame.size.height - 166) / 4
            let layout = collectionViewModelos.collectionViewLayout as! UICollectionViewFlowLayout
            layout.itemSize = CGSize(width: width, height: height)
        }
    }

    @objc func deviceRotated(){
        recalcularSizeCeldas()
    }

}


extension VC_catalogo_modelos: UICollectionViewDataSource{

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.arrayModelos.count
    }

    //funcion que recorre todas las celdas antes de presentarlas
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        //instancio el modelo de celta
        let cell = collectionViewModelos.dequeueReusableCell(withReuseIdentifier: "cellModelos", for: indexPath) as! VC_catalogo_modelos_cell
        return cell
    }

    //cuando se pincha un producto del tableview
    internal func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let cell = collectionViewModelos.dequeueReusableCell(withReuseIdentifier: "cellModelos", for: indexPath) as! VC_catalogo_modelos_cell

    }

}

extension VC_catalogo_modelos: UIViewControllerTransitioningDelegate, UINavigationControllerDelegate {
    func navigationController(
        _ navigationController: UINavigationController,
        animationControllerFor operation: UINavigationController.Operation,
        from fromVC: UIViewController,
        to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        //This tells our navigation controller the style that we want to animate our transition between view controllers

        let simpleOver = SimpleOver(direction: .right)
        simpleOver.popStyle = (operation == .pop)
        return simpleOver
    }
}

最佳答案

我的问题是,当我在横向位置使用 ipad 进入 viewcontroller 时,一切都是正确的,当我以纵向位置进入 viewcontroller 时,一切都是正确的,但是一旦我进入 view controller 并转动 ipad , 都失去了位置,我不知道为什么......谢谢。

you can see a video capture of the problem here

关于ios - 设置列数和固定行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57345711/

相关文章:

ios - 运行 add2app ios 时使用本地 flutter 引擎

ios - 如何使用 iOS Core Bluetooth 在 BLE 上完全断开连接

android - 这有可能覆盖 fragment 中的屏幕方向吗

ionic-framework - 屏幕方向锁定在 ionic 中不起作用

ios - 将 Game over 实现到 swift 游戏中

ios - 将 ReactiveCocoa 3 和 4 与核心数据一起使用

ios - 需要名为 "-568h@2x.png"的启动镜像才能在 Retina 4 设备上以 native 分辨率运行

ios - 添加目标时出现 Swift 错误

ios - 将图像 URL 从 Firebase 附加到数组

ios - iPhone 的中间 x 和 y 坐标