How to Center an App Icon Image Vertically in a UITableViewCell

Hello!

I'm making a list of app icons for users to choose, but when I increase or decrease the font size, the image is still in the same spot and isn't centered vertically with the text. I have it initialized with a frame with hard-coded values, but I was wondering if there was a better way of doing it, such as with constraints or some sort of image scaling.

I've provided code blocks and an image of what is happening.

ImageView Configuration

// App Icon Image
UIImageView *appIconImageView = [[UIImageView alloc] initWithFrame: CGRectMake(12.5, 17, 22.5, 22.5)];
    
// Configurations
UIImageSymbolConfiguration *multicolorConfiguration = [UIImageSymbolConfiguration configurationPreferringMulticolor];
UIImageSymbolConfiguration *sizeConfiguration = [UIImageSymbolConfiguration configurationWithScale: UIImageSymbolScaleSmall];
UIImageSymbolConfiguration *appIconConfiguration = [multicolorConfiguration configurationByApplyingConfiguration: sizeConfiguration];
    
appIconImageView.preferredSymbolConfiguration = appIconConfiguration;
appIconImageView.contentMode = UIViewContentModeScaleAspectFill;
    
self.appIconImage = appIconImageView;
[appIconImageView release];

ImageView Constraints

        [self.appIconImage.firstBaselineAnchor constraintEqualToAnchor: self.contentView.firstBaselineAnchor constant: 5.0],
        [self.appIconImage.leadingAnchor constraintEqualToAnchor: self.contentView.layoutMarginsGuide.leadingAnchor],
        
        // Label
        [self.colorLabel.leadingAnchor constraintEqualToAnchor:self.appIconImage.trailingAnchor constant: 10],
        [self.colorLabel.trailingAnchor constraintEqualToAnchor:self.contentView.layoutMarginsGuide.trailingAnchor],
        [self.colorLabel.topAnchor constraintEqualToAnchor: self.contentView.layoutMarginsGuide.topAnchor],
        [self.colorLabel.bottomAnchor constraintEqualToAnchor: self.contentView.layoutMarginsGuide.bottomAnchor],

Image

Hello dteakell,

Thanks for the code snippets and screenshot. Looks like you're matching firstBaselineAnchor of the app icon to that of the row. Is there a reason why you're choosing to do that? You can simply center them vertically with one another like this:

[self.appIconImage.centerYAnchor constraintEqualToAnchor:self.colorLabel.centerYAnchor];

Thank you for your patience,

Richard Yeh  Developer Technical Support

How to Center an App Icon Image Vertically in a UITableViewCell
 
 
Q