1

I'm experiencing a strange issue with my UIKit app. I have a UITableView that contains a UICollectionView. The UITableView gets reloaded every few seconds, but after each reload, the size of the SF Symbols inside the UICollectionView cells changes slightly, as you can see in this example (I've set the reload timer to 1 second for demonstration purposes).

Demo

I'm not sure if this issue is caused by my unusual table view/collection view configuration or if it's an Apple bug. Does anyone know a solution to this problem?

Here's the relevant code:

This is inside my ViewController:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

...

productsCollectionView.reloadData()
return cell
}


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "verbindungCollectionViewCell", for: indexPath) as! verbindungCollectionViewCell
    
    if legArray[tempIndexPathFromTableView.row][0][indexPath.row] is PublicLeg {
        //Public Leg
        var tempPublicLeg = legArray[tempIndexPathFromTableView.row][0][indexPath.row] as! PublicLeg
        cell.serviceLabel.text = tempPublicLeg.line.label
        cell.serviceImageView.image = getProductSymbol(productType: productToTransportType(product: tempPublicLeg.line.product!), designPack: .automatic)
        cell.setCancelled(tempPublicLeg.isCancelled)
        if tempPublicLeg.isCancelled == true {
            
        }
    } else {
        //Individual Leg (Walk)
        var tempIndLeg = legArray[tempIndexPathFromTableView.row][0][indexPath.row] as! IndividualLeg
        cell.serviceImageView.image = getProductSymbol(productType: .walk, designPack: .automatic)
        cell.serviceLabel.text = tempIndLeg.departure.getDistanceText(CLLocation(latitude: CLLocationDegrees(tempIndLeg.arrival.coord?.lat ?? 0)/1000000, longitude: CLLocationDegrees(tempIndLeg.arrival.coord?.lon ?? 0)/1000000))
    }
    return cell
}

This is the function that returns the SF Symbols:

func getProductSymbol(productType: TransportType, designPack: DesignPack) -> UIImage? {

...Determinate color, symbol etc...

    if colorType == .singleColor {
    let config = UIImage.SymbolConfiguration(hierarchicalColor: colors[0])
    let image = UIImage(systemName: symbolName, withConfiguration: config)?.withRenderingMode(.automatic)
    
    return image
} else if colorType == .multipleColor {
    let config = UIImage.SymbolConfiguration(paletteColors: colors)
    let image = UIImage(systemName: symbolName, withConfiguration: config)?.withRenderingMode(.automatic)
    return image
} else {
    return nil
}
}

And here is my UIViewController in Storyboard:

enter image description here

Thank you all in advantage.

Victor Lobe
  • 355
  • 3
  • 12
  • 1
    You have a ***LOT*** going on there -- so, two quick suggestions... 1: use your `SymbolConfiguration` to explicitly set the size of the SF Symbol, and 2: Try to reproduce the issue under much, much simpler conditions. Start with a `UIImageView` by itself, where you are updating the image every second. If it doesn't show the same sizing problem, make it a subview of another view. Keep adding *minor* complexity changes until you have code that clearly and easily reproduces the problem so it can be debugged from there. – DonMag Mar 24 '23 at 12:43
  • Thank you for your response! Unfortunately, I'm unable to adjust the size of the SFSymbols because I'm already using the color attribute. As far as I know, it's only possible to modify one aspect of the symbol at a time. – Victor Lobe Mar 26 '23 at 02:04
  • Okay, i reproduced the issue successfully and it was the constraints, I am going to find a way to fix it. – Victor Lobe Mar 26 '23 at 02:43
  • 1
    Victor -- if you found constraint issue(s), then sure, you want to fix that. However... you can certainly apply both color and size (and other) attributes to SFSymbols - take a look at this Apple docs page: https://developer.apple.com/documentation/uikit/uiimage/configuring_and_displaying_symbol_images_in_your_ui – DonMag Mar 26 '23 at 12:42
  • Oh my god! I made several approaches but never thought of this. I now „fixed“ it by simply removing the constraints of the label und ImageView inside the CollectionViewCell. – Victor Lobe Mar 26 '23 at 15:00

1 Answers1

0

First I wouldn't place productsCollectionView.reloadData() within UITableView, cellForRowAt. This will cause the collectionView to do a full layout reload foreach item in the tableView.

Also try putting a breakpoint in getProductSymbol, does it get called over and over again, even after the datas been reloaded?

If so that's the problem.

OneCommentMan
  • 147
  • 1
  • 6