How to get 1 pixel width borders in UICollectionView cells? (code provided)

Greg

How can I get a pixel perfect one (1) pixel with border line in a UICollectionView (e.g. to make a month calendar). The issue is that where cells meet, their borders meet, so it is effectively 2 pixels (not 1 pixel).

Therefore this is a CollectionView layout related issue/question. As the way each cell puts it own 1 pixel border gives rise, when the cells all meet up with zero spacing, to what looks like a 2 pixels border overall (except for outer edges of the collectionView which would be 1 pixel)

Seeking what approach/code to use to solve this.

Here is the custom UICollectionViewCell class I use, and I put borders on the cells in here using drawrect.

import UIKit

class GCCalendarCell: UICollectionViewCell {

    @IBOutlet weak var title : UITextField!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    override func drawRect(rect: CGRect) {
        self.layer.borderWidth = 1
        self.layer.borderColor = UIColor.redColor().CGColor
    }

}
Anthony Mattox

There are a few strategies you can take to solve this issue. Which is best depends on some of the details of your layout.

Option 1: Half Borders

Draw borders of 1/2 the desired width around each item. This has been suggested. It achieves the correct width for borders between items, but the borders around the edge of the collection will also be half the desired width. To solve this you can draw a border around the entire collection, or sections.

If you're month layout only renders days in a given month, meaning each month has a unique non-rectangular shape, fixing the edge borders is more complicated than it's worth.

Option 2: Overlapping Cells

Draw the full border around each item but adjust the frames so they overlap by half of the width of the borders, hiding the redundancy.

This is the simplest strategy and works well, but be aware of a few things:

  • Be mindful that the outer edges of the layout are also slightly changed. Compensate for this if necessary.
  • The borders will overlap, so you can't use semi-transparent borders.
  • If not all the borders are the same color, some edges will be missing. If there is just a single cell with a different border, you can adjust the item's z-index to bring it on top of the other cells.

Option 3: Manual Drawing

Rather than using borders on the cell at all, you can add a border around the container and then manually draw the borders in the superview. This could be accomplished by overriding drawRect(_:) and using UIBezierPaths to draw horizontal and vertical lines at the intervals of the cells. This is the most manual option. It gives you more control, but is much more work.

Option 4: Draw Only Some Borders in Each Cell

Rather than assigning the layer's border width and border color, which draws a border around the entire view, selectively draw edges of cells. For example: each cell could only draw it's bottom and right edge, unless there is no cell above or to the right of it.

This can again lead to complications if you have an irregular grid. It also requires each cell to know about it's context in the grid which requires more structure. If you go this route, I would suggest subclassing UICollectionViewLayoutAttributes and adding additional information about which edges should be drawn. The edge drawing itself can again be done by creating UIBezierPath's in drawRect(_:) for each edge.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get a pixel width of a string in android?

UICollectionView cells do not take up width of UICollectionView

How to delete cells on UICollectionView

How to remove UICollectionView cells

How to get indexpaths of cells in UICollectionView for specific content offset?

How to set css width or left attributes to get pixel perfect result?

How to get image width and height pixel, and make the box as same size?

How to get width of a html element and convert from pixel to persentage

Win32api how to get pixel width of char

Mystery how google makes borders half as thick but still set to 1 pixel

How to add event to UICollectionView cells?

How to center horizontally UICollectionView Cells?

How to center align the cells of a UICollectionView?

How to 'fit' the cells of UICollectionView FlowLayout?

How to NOT reuse cells in UICollectionView - Swift

How to avoid UICollectionView repeating cells?

How to make UICollectionView cells movable?

How do I put borders on specific cells

How to set different colored/width borders on a layout

How to stretch image to the screen borders by width?

How to get browser width using JavaScript code?

UICollectionView, full width cells, allow autolayout dynamic height?

Get the maximum pixel width of the browser in Javascript

How do I get the frames of cells in a UICollectionView before the screen has been presented?

How to adapt this grid to the width of the cells?

How to draw a line wich is 1 pixel width and many pixels length without anti-aliasing?

UICollectionview and cells

How to change space between cells in UICollectionView?

How to create horizontally dynamic UICollectionView cells? Swift