简体   繁体   中英

UIButton image working on simulator but not on the device

I have an app where there is a UITableView populated UIButtons that are created programatically. The code below shows how one UIButton is created.

It works perfectly on the simulator but not on the device. On the simulator the buttons are displayed with the buttonImage.png shown but on the device the image is not there. All other aspects of the buttons are correct.

The image has been added to the resources correctly as far as I can tell. Any ideas?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
if (cell ==nil)
{
    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0,0,160, 160)];
[button setBackgroundImage:[UIImage imageNamed:@"buttonImage.png"] forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorWithRed:19/255.0f green:73/255.0f blue:17/255.0f alpha:1] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"arial" size:20];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"button" forState:UIControlStateNormal];

//more buttons created as needed

NSMutableArray *buttonArray = [[NSMutableArray alloc] init];
[array addObject:button];
//more buttons added to array as needed

[cell addSubview:[buttonArray objectAtIndex:indexPath.row]];

return cell;

}

检查您的映像名称,因为模拟器不区分大小写,但设备区分大小写。

I ran into a similar problem when I renamed an icon in my application, using the XCode navigator window. All was fine in the simulator, but when I (eventually) tested on my device the icon would not show up. I verified the spelling, and case, numerous times (after reading the above accepted answer), and I even copy/pasted the icon name over... no dice.

I tried cleaning the app and rebuild... no dice.

Finally I closed XCode completely and relaunched from 'Launchpad',... and it worked.

It was a frustrating hour. Feels, to me, like there's a bug in there somewhere.

Your code is not at fault. I am sure you have tested your app on non retina simulator & it is showing button.

So now when you test it on device, I am sure you are testing on a retina device & you have not provided the 2x version of your button image.

Solution:: add a 2x version of image to your resource.

Example:: yourImage.png (32x32), then add another image to resource with name yourImage@2x.png (64x64)

That solves your problem.

Hope that helps.

Try to make these changes in your code:

Change control event

[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchInside];

Add Button in content view of cell

[cell.contentview addSubview:[buttonArray objectAtIndex:indexPath.row]];

尝试从设备中删除已安装的应用,然后重新运行

检查拼写,并通过button.allowUserInteraction = true允许用户交互

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