Hello, I have been having trouble with configuring the systemImage size. I took a number of approaches but couldn't manage to set a size to the systemImage within the collectionView cell. Because of this, the text which is supposed to be below the image, clashes with the image due to its large size within the cell. The last approach I took with the code I'm sharing, I get an empty cell. Otherwise, I can't find a way to change the systemImage size. Could you please advise me regarding the configuration of systemImage ?
Many Thanks.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == collectionViewEffects {
let cellEffects = collectionView.dequeueReusableCell(withReuseIdentifier: "EffectsCell", for: indexPath) as! CustomEffectsCell
collectionView.backgroundColor = .systemGray6
if indexPath.row == 0 {
cellEffects.effectsLabel = "A"
let config = UIImage.SymbolConfiguration(pointSize: 0, weight: .ultraLight)
let cellImage = UIImage(systemName: "slider.horizontal.3", withConfiguration: config)
let width = ((cellImage?.size.width)!)/4
let height = ((cellImage?.size.height)!)/4
guard let context = UIGraphicsGetCurrentContext() else {
print("could not get graphics context")
return cellEffects
}
context.setStrokeColor(UIColor.yellow.cgColor)
context.setLineWidth(2)
context.stroke(cellImage?.cgImage as! CGRect)
cellImage?.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
cellEffects.effectsImage = cellImage
self.view.addSubview(cellEffects)
}
I'm not sure if "return UICollectionViewCell()" was also relevant in line 22.
The line is needed just to silence the compiler. You need to return someting of type UICollectionViewCell, in case `collectionView !== collectionViewEffects`. Even if that would never happen, compiler needs it.
I'm getting are (57.0, 44.6). I'm roughly aiming at (15.0, 15.0).
You can control the size of the image by changing the pointSize passed to UIImage.SymbolConfiguration.
let config = UIImage.SymbolConfiguration(pointSize: 15, weight: .ultraLight)
I guess the reason you suggested me to use 2 different settings for the cell is to have a different size for the backgroundColor.
Size for the background color may be one of the different settings, there may be others.
As cells are reused among the same Reuse Identifer cells, you need to consider the possibitily that the row-0-setting cell may be reused for non-row-0 cells.
Please tell me when you find something unclear in your next steps.