UICollectionViewFlowLayout minimumLineSpacing not

2019-09-07 03:37发布

问题:

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;