【发布时间】:2018-07-26 08:07:31
【问题描述】:
集合视图通过自动布局获得正确的大小,但集合视图上的视图大小无法正常工作集合视图上的视图从情节提要设计中获取大小。这取决于您在情节提要设计中设置的大小。它在 Iphone 中完美运行,但在 Ipad 中无法运行。我试图在部分设置项目的大小,但它不起作用。 项目的保管箱链接是here。
let isPad = UIDevice.current.userInterfaceIdiom == .pad
import UIKit
class AchivementVC: UIViewController {
@IBOutlet weak var clcAchivement:UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func btnBackClick(sender:UIButton)
{
self.navigationController?.popViewController(animated: true)
}
}
extension AchivementVC:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "achiveCell", for: indexPath) as! AchiveClcCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
print(collectionView.frame)
if(isPad)
{
return CGSize(width: (collectionView.frame.size.width * 0.5)-30, height: (collectionView.frame.size.width * 0.45)-30)
}
else
{
return CGSize(width: (collectionView.frame.size.width * 0.5)-10, height: (collectionView.frame.size.width * 0.45)-10)
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
if(isPad)
{
return 30
}
else
{
return 10
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
if(isPad)
{
return 30
}
else
{
return 10
}
}
}
class AchiveClcCell:UICollectionViewCell
{
override func awakeFromNib() {
self.setNeedsLayout()
}
override func layoutSubviews() {
}
}
【问题讨论】:
-
你能写一个minimal reproducible example并将它复制到你的问题正文中,或者添加一些问题的截图吗?
-
我正在展示完整的演示,请参阅 dropbox 上的项目
标签: swift uicollectionview autolayout