Collectionview 显示不同单元格与不同 iPad 设备的自定义日历
我正在尝试在我的应用程序中创建自定义日历,并添加了以下代码。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout:
UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width/7, height: 36)
}
但在不同的 iPad 设备中,工作日显示的时间多于 7 和 7。有些 iPad 显示一周中的 5 天。它根据屏幕尺寸更改单元格尺寸。但我想添加 &所有 iPad 屏幕宽度的天数。然后我尝试了以下方法
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)
}
,使用此方法后,大多数 iPad 设备的电池续航时间正好为 7 天。但某些屏幕宽度为 1024 的设备(iPad 5、6、7 代)仍每周显示 6 天。我用模拟器尝试过。
I’m trying to create a custom calendar in my app and I added the following code.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout:
UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width/7, height: 36)
}
But in different iPad devices, the weekdays show more than 7 & and some iPad show 5 days of the week. It's changing the cell size depending on the screen size. But I want to add & days for all iPad screen widths. Then I tried the following
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)
}
For using this most of the iPad devices are getting exactly 7 days cell. But some devices (iPad 5th, 6th, 7th generation) with 1024 screen width still showing 6 days a week. I tried with the simulator.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的答案可能暂时有效,但我建议不要使用硬编码值,例如
173、20、7 等
,如果屏幕尺寸甚至方向发生变化,这些值可能不起作用。主要要弄清楚的是在获取单元格尺寸之前您实际上需要使用多少
可用宽度
。这就是我要做的:
这将为您提供手机肖像中的以下内容:
横向手机
iPad
在所有情况下,您都可以在不使用随机数的情况下获得 7 列
这是完整的解决方案:
Your answer might work for the time being, however I would recommend against using hard-coded values like
173, 20, 7 etc
which might not work if screen dimensions change or even the orientation.The main thing to figure out is how much
available width
you actually have to work with before getting the cell dimensions.Here is what I would do:
This will give you the following in phones portrait:
Landscape phones
iPad
In all cases you get 7 columns without using random numbers
Here is the full solution:
我通过以下代码得到了完美的解决方案。
func collectionView(_ collectionView: UICollectionView, 布局 collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGS尺寸{
返回 CGSize(宽度: (self.contentView.frame.width - 173)/7, 高度: 45)
}
I got the perfect solution with the following code.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (self.contentView.frame.width - 173)/7, height: 45)
}