簡體   English   中英

UIButton不顯示圖像

[英]UIButton doesn't show image

我有一個UITableViewCell ,我要向其中添加五個UIButtons 我以編程方式創建按鈕、添加操作和圖像。 按鈕放置在正確的位置,動作有效,標簽已設置,但它們很清楚。 圖片不顯示。

這就是我用來創建其中一個按鈕並將它們添加到單元格的方法,此代碼位於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];
        }

我試過添加: [button setEnabled:YES]; , [button setHidden:NO]; , 和[button setNeedsDislpay]; 到沒有運氣的過程。 如何獲得顯示圖像的按鈕?

編輯:我的圖像存在,創建了UIImage ,我可以將UIImageUIButton中拉出來並將其保存到文件中。 我已經檢查了控制外觀的UIButton的每個屬性並更改了它們。 我還嘗試了UIButtonTypeRoundedRectUIButtonTypeAddContact類型的空UIButton 我現在相信這與UITableViewCell不喜歡UIButtons或我如何將UIButton添加到UITableViewCell有關。

確保在復制文件時已將項目檢查為目標成員資格。 它幫助我解決了問題。

我的錯誤是將圖像拖到作為 pod 一部分的 images.xcassets 文件中,而不是主項目。 它在故事板中顯示良好,但沒有出現在應用程序中。

如果有人仍在尋找答案:一旦圖像位於 assets.xcassets 中,那么我需要將它包含在目標構建階段部分的“復制包資源”區域中。 然后我還必須設置一個透明的顏色,這樣它就不會被遮擋。

項目->目標->構建階段->復制捆綁資源->“+”->“添加其他”->在查找器中選擇圖像。

我知道這是一篇舊帖子,但我遇到了同樣的問題,這是由於isHiddenisUserInteractionEnabled設置為隱藏按鈕的值。

將它們設置為YESNO時必須小心。

如果有人正在尋找答案,請仔細檢查您是否已將圖像復制到項目目錄中的 Assets.xcassets 而不是 pods 目錄中,其次我在 Assets.xcassets 圖像屬性中使用渲染作為原始圖像

它肯定能找到圖像嗎? 他們是否包含在項目中?

只是為了測試,嘗試使用您已經在其他地方使用過的圖像而不是 Comp-Completed.png - 如果有效,您就知道問題在於它無法找到該文件。

檢查您的圖像是否存在並已導入。 在我看來,除了圖像名稱之外,代碼是相同的。 更干凈一點:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM