简体   繁体   中英

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 .

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

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.

In case anyone is still looking for an answer: Once the image was in assets.xcassets then I needed to include it in the "copy bundle resources" areas of the Build Phases part of the Target. Then I also had to set a transparent color so that it wasn't obscured.

project->targets->build phases->copy bundle resources-> "+" -> "add other"->select the image in the finder.

I know it is an old post but I came across the same problem and it was due to isHidden and isUserInteractionEnabled set to values to hide the button.

You have to be careful when to set them to YES or NO .

In case anyone is looking for answer double check if you have copied the image to Assets.xcassets in project directory not in pods directory, Second i used render as orignal image in Assets.xcassets image properties

Can it definitely find the images? Are they included in the project?

Just to test, try using an image you've already used somewhere else instead of Comp-Completed.png - if that works, you know that the problem is that it's failing to find that file.

Check whether your images exist and are imported. Also it looks to me that besides of the image name the code is identical. A bit cleaner as:

UIImage *buttonImage;
if ([[self.compCompletion objectForKey:@"Key" isEqualToString:@"YES"]) {
    buttonImage = [UIImage imageNamed:@"Comp-Completed.png"];
} else {
    buttonImage = [UIImage imageNamed:@"Comp-Uncompleted.png"];
}

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:buttonImage] forState:UIControlStateNormal];
[button setContentMode:UIViewContentModeScaleToFill];
[cell.contentView addSubview:button];
UIImage * normalImage = [UIImage imageNamed:@"Comp-Uncompleted.png"];
UIImage * selectedImage = [UIImage imageNamed:@"Comp-Completed.png"];

UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
button.frame = CGRectMake(11, 6, 21, 21);
button.tag = 1;
[button addTarget:self action:@selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:normalImage forState:UIControlStateNormal];
[button setImage:selectedImage forState:UIControlStateSelected];
[cell.contentView addSubview:button];

if ([[self.compCompletion objectForKey:@"Key" isEqualToString:@"YES"]) {
    button.selected = YES;
} else {
    button.selected = NO;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM