Estoy tratando de crear un calendario personalizado en mi aplicación y agregué el siguiente código.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: collectionView.frame.size.width/7, height: 36) }Pero en diferentes dispositivos iPad, los días de la semana muestran más de 7 y algunos iPad muestran 5 días de la semana. Está cambiando el tamaño de la celda según el tamaño de la pantalla. Pero quiero agregar & días para todos los anchos de pantalla de iPad. Entonces probé lo siguiente
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { if UIDevice().userInterfaceIdiom == .pad { if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad && (UIScreen.main.bounds.size.height == 1366 || UIScreen.main.bounds.size.width == 1366)) { print("iPad Pro : 12.9 inch") return CGSize(width: collectionView.frame.width/7 + 20, height: 36) } else if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad && (UIScreen.main.bounds.size.height == 1024 || UIScreen.main.bounds.size.width == 1024)) { print("iPad 2 || iPad Pro : 9.7 inch || iPad Air/iPad Air 2 || iPad Retina ") return CGSize(width: collectionView.frame.width/7 - 5, height: 36) } else if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad && (UIScreen.main.bounds.size.height == 1180 || UIScreen.main.bounds.size.width == 1180)) { print("iPad Air 4th gen") return CGSize(width: collectionView.frame.width/7 + 5, height: 45) } else if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad && (UIScreen.main.bounds.size.height == 1194 || UIScreen.main.bounds.size.width == 1194)) { print("iPad Pro 11") return CGSize(width: collectionView.frame.width/7 + 5, height: 45) } else if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad && (UIScreen.main.bounds.size.height == 1112 || UIScreen.main.bounds.size.width == 1112)) { print("iPad Air 3") return CGSize(width: collectionView.frame.width/7, height: 45) } else { print("iPad 3") return CGSize(width: collectionView.frame.width/7, height: 45) } } return CGSize(width: collectionView.frame.width/7, height: 45) }Para usar esto, la mayoría de los dispositivos iPad obtienen exactamente 7 días de celda. Pero algunos dispositivos (iPad de 5.ª, 6.ª y 7.ª generación) con un ancho de pantalla de 1024 siguen mostrando 6 días a la semana. Probé con el simulador.
Obtuve la solución perfecta con el siguiente código.
func collectionView(_ collectionView: UICollectionView, diseño collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: (self.contentView.frame.width - 173)/7, height: 45) }