繁体   English   中英

UIButton图像在模拟器上工作但不在设备上工作

[英]UIButton image working on simulator but not on the device

我有一个应用程序,其中有一个以编程方式创建的UITableView填充的UIButtons。 下面的代码显示了如何创建一个UIButton

它在模拟器上完美运行,但在设备上无效。 在模拟器上显示按钮,并显示buttonImage.png,但在设备上图像不存在。 按钮的所有其他方面都是正确的。

据我所知,图像已正确添加到资源中。 有任何想法吗?

- (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;

}

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

当我使用XCode导航器窗口在我的应用程序中重命名图标时,我遇到了类似的问题。 在模拟器中一切都很好,但是当我(最终)在我的设备上测试时,图标不会显示出来。 我已经多次验证了拼写和案例(在阅读了上面接受的答案之后),我甚至将图标名称复制/粘贴到...没有骰子。

我尝试清理应用程序并重建......没有骰子。

最后,我完全关闭了XCode,并从“Launchpad”重新启动......,它确实有效。

这是一个令人沮丧的时刻。 对我来说,感觉就像在某处有一个小虫。

你的代码没有错。 我相信你已经在非视网膜模拟器上测试了你的应用程序,它正在显示按钮。

所以现在当你在设备上进行测试时,我确信你正在测试视网膜设备并且你没有提供2x版本的按钮图像。

解决方案::将2x版本的图像添加到资源中。

示例:: yourImage.png(32x32),然后将另一个图像添加到名为yourImage@2x.png(64x64)的资源

这解决了你的问题。

希望有所帮助。

尝试在代码中进行以下更改:

改变control event

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

在单元格的内容视图中添加按钮

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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