在Ipad上自动布置奇怪的行为

时间:2018-07-26 08:07:32

标签: swift uicollectionview autolayout

使用自动布局可以正确调整集合视图的大小,但集合视图上的视图大小无法正常工作,而从情节提要设计中获取大小的集合视图上的视图却无法正常工作。这取决于您在情节提要设计中设置的大小。它可以在Iphone中完美运行,但不能在Ipad中运行。我试图在部分设置项目的大小,但无法正常工作。 项目的保管箱链接为here

code is:-

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() {


    }
}

1 个答案:

答案 0 :(得分:0)

覆盖super.layoutSubviews()时应致电layoutSubviews