[英]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. 我有一个应用程序,其中有一个以编程方式创建的
UITableView
填充的UIButtons。 The code below shows how one UIButton
is created. 下面的代码显示了如何创建一个
UIButton
。
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.
在模拟器上显示按钮,并显示buttonImage.png,但在设备上图像不存在。 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. 当我使用XCode导航器窗口在我的应用程序中重命名图标时,我遇到了类似的问题。 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. 最后,我完全关闭了XCode,并从“Launchpad”重新启动......,它确实有效。
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. 所以现在当你在设备上进行测试时,我确信你正在测试视网膜设备并且你没有提供2x版本的按钮图像。
Solution:: add a 2x version of image to your resource. 解决方案::将2x版本的图像添加到资源中。
Example:: yourImage.png (32x32), then add another image to resource with name yourImage@2x.png (64x64) 示例:: yourImage.png(32x32),然后将另一个图像添加到名为yourImage@2x.png(64x64)的资源
That solves your problem. 这解决了你的问题。
Hope that helps. 希望有所帮助。
Try to make these changes in your code: 尝试在代码中进行以下更改:
Change control event
改变
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
允许用户交互
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.