簡體   English   中英

如何將UITableView背景設置為圖像?

[英]How to set UITableView background to image?

我注意到許多應用程序都有UITableView的自定義背景圖像(或者將背景設置為顏色。)

我在UITableView類中找不到任何API來影響這一點,我在界面構建器中的嘗試失敗了(嘗試設置UIView的顏色)

有什么建議么? 我見過帶有圖片的第三方應用,所以我知道可以做到。

我自己也遇到了同樣的問題,但我找到了TomSwift觸及的方法。 正如他所提到的,UITableView有一個backgroundView屬性,旨在滿足您的需求。 如果你想設置背景顏色,你應該使用backgroundColor屬性,正如其他人所說的那樣。 所以:

// Set an image as the background of a UITableView called 'tableView'
UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MyImage.png"]];
[tableView setBackgroundView:bg];

要么:

// Set a solid color as the background of a UITableView called 'tableView'
// In this case I chose red
[tableView setBackgroundColor:[UIColor redColor]];
UIImageView *image = [[UIImageView alloc] initWithImage:
                                    [UIImage imagenamed:@"no_image.png"]];
[self.view addSubview:image];
UITableView *tableview = [[UITableView alloc] init];
[image addSubview:tableview];
tableview.backgroundcolor = [UIColor clearColor];

在您要為圖像設置背景顏色的代碼中添加此行

[YourView(或imageView)setbackgroundColor:[UIColor groupTableViewBackgroundColor]];

謝謝

嘗試這個。 這個對我有用。

CALayer *background = [CALayer layer];
background.zPosition = -1;
background.frame = self.view.frame;
background.contents = [[UIImage imageNamed:@"background.jpg"] CGImage];
[tableView.layer addSublayer:background];

您需要添加圖像視圖並將表格背景顏色設置為清除顏色,請參閱此鏈接以獲取tutoria l

這就是我做到的。 可能不是最好的方式,但效果還不錯。

UIImage *bg = [UIImage imageNamed:@"bg"];
[bg drawInRect:self.view.window.frame];
UIImageView *bgView = [[UIImageView alloc] initWithFrame:self.view.window.frame];
bgView.image = bg;

activityTable.backgroundView = bgView;  //activityTable = UITableView

設置tableView的backgroundView屬性。 您可以將其設置為已設置自定義backgroundColor的視圖,也可以將其設置為已設置圖像的UIImageView。

GhostRider的替代解決方案無編碼

通過界面構建​​器創建UiImageView,將您的圖像放入資源中。 通過使用Inspector在UIImageView Image屬性中設置它。 選擇表視圖並將其設置為要清除的背景顏色(對於此,使用屬性檢查器將不透明度設置為0)。

暫無
暫無

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

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