[英]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.