Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton doesn't show image

I have a UITableViewCell that I'm adding five UIButtons to. I programmatically create the buttons, add an action, and an image. The buttons are placed in the correct spot, the action works, and the tag is set, but they are clear. The images do not show.

This is what I'm using to create one of the buttons and add them to the cell, this code is in the tableView:cellForRowAtIndexPath: :

if ([[self.compCompletion objectForKey:@"Key" isEqualToString:@"YES"]) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(11, 6, 21, 21);
            button.tag = 1;
            [button addTarget:self action:@selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
            [button setImage:[UIImage imageNamed:@"Comp-Completed.png"] forState:UIControlStateNormal];
            [button setContentMode:UIViewContentModeScaleToFill];
            [cell.contentView addSubview:button];
        }
        else {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(11, 6, 21, 21);
            button.tag = 1;
            [button addTarget:self action:@selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
            [button setImage:[UIImage imageNamed:@"Comp-Uncompleted.png"] forState:UIControlStateNormal];
            [button setContentMode:UIViewContentModeScaleToFill];
            [cell.contentView addSubview:button];
        }

I've tried adding: [button setEnabled:YES];, [button setHidden:NO];, and [button setNeedsDislpay]; to the process with no luck. How do I get the button to display the image?

EDIT: My image exists, the UIImage created, and I can pull the UIImage out of the UIButton and save it to a file. I've gone through each property of the UIButton that controls appearance and changed them. I also tried an empty UIButton of type UIButtonTypeRoundedRect and UIButtonTypeAddContact. I'm now believe that this has something to do with the UITableViewCell not liking UIButtons, or how I'm adding the UIButton to the UITableViewCell.

like image 266
Brandon Mcq Avatar asked Dec 05 '25 22:12

Brandon Mcq


2 Answers

Make sure that you have checked your project as Target Membership when copying the files. It helped me solve the problem.

like image 55
Ben Quan Avatar answered Dec 07 '25 14:12

Ben Quan


My mistake was dragging the images into an images.xcassets file that was part of a pod, NOT the main project. It showed fine in the storyboard, but did not appear in the app.

like image 40
Gabriel Jensen Avatar answered Dec 07 '25 13:12

Gabriel Jensen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!