UICollectionViewFlowLayout minimumLineSpacing not applied to whole collection view - Swift

穿精又带淫゛_ 提交于 2019-12-12 01:13:40

问题


I'm creating UIView subclass for calendar, wherein UICollectionView with flow layout is used to display dates. The code is :

    override func draw(_ rect: CGRect)
    {
        super.draw(rect)
        .
        .

        configureCalendarView()
        .
    }



    func configureCalendarView()
    {
        cellWd = Double(self.frame.size.width)/7.0

        let layout = UICollectionViewFlowLayout.init()
        layout.minimumLineSpacing = 0.5
        layout.minimumInteritemSpacing = 0.0
        layout.sectionInset = UIEdgeInsetsMake(1, 0, 0, 0)

        collectionV = UICollectionView.init(frame: CGRect(x: 0, y:90, width:self.frame.size.width, height:CGFloat(cellWd * 5 + 3.5)), collectionViewLayout: layout)
        collectionV?.backgroundColor = self.backgroundColor
        collectionV?.isScrollEnabled = false
        collectionV?.register(DayCell.classForCoder(), forCellWithReuseIdentifier:reuseID)
        collectionV?.dataSource = self;
        collectionV?.delegate = self;
        collectionV?.backgroundColor = calBgColor  //lightgray
        self.addSubview(collectionV!)
    }

Everything is working fine but minimum line spacing is not having any effect between 2nd and 3rd row. Below is the screenshot :

Whats going wrong ? Any help would greatly be appreciated . Thank you !


回答1:


layout.minimumLineSpacing = 0.5 translates to 0.5point * scale = pixels.

So if you are running on iphone 6+ 7+ with 3.0 scale, that 0.5 will be 1.5 pixels. And on 2x scale that will be 1 pixel. To correct this. add this:

layout.minimumLineSpacing = 1.0 / [UIScreen mainScreen].scale; This way you guaranteed always get 1 pixel spacing.

Edit:

layout.minimumLineSpacing = 1.0f;

CGFloat collectionWidth = self.frame.size.width;
CGFloat days = 7.0f;
CGFloat spacing = 1.0f;

CGFloat itemHeight = floorf((collectionWidth/days) - spacing;


来源:https://stackoverflow.com/questions/41675487/uicollectionviewflowlayout-minimumlinespacing-not-applied-to-whole-collection-vi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!