gpt4 book ai didi

ios - 设置列数和固定行数

转载 作者:行者123 更新时间:2023-11-28 09:21:42 25 4
gpt4 key购买 nike

在我的项目中,我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com