簡體   English   中英

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

[英]How to set the background of a UITableView to an image?

我正在使用UITableViewController來上傳表格。 我有一個帶有UITableView的Nib文件。現在我想要從界面構建器或從TableViewController到圖像設置tableView的背景。

怎么做。

好的,所以我在控制器中創建了一個UIImage。 現在當我添加我需要添加的地方時。

當我嘗試添加它並將tableView的顏色設置為clearColor時,它只顯示Image而不是表格,盡管我確保將圖像發送到所有視圖的背面。

伙計請注意,我沒有處理UIView並添加tableView作為其子視圖但我正在處理UITableView。

將UIImageView放在UITableView后面,然后執行以下操作:

tableView.backgroundColor = [UIColor clearColor];

以某種方式玩我能夠找到一種方法。

self.tableView.backgroundColor = [UIColor clearColor];

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:imageData]];

self.tableView.backgroundColor = [UIColor clearColor];

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@“parentViewBackground.png”]];

圖像大小應該是
2x - 640 * 960
1x - 320 * 480

尺寸小於上面,它將平鋪。

backgroundColor = [UIColor colorWithPatternImage: image]方法rkbang輪廓效果很好。 這是通過向parentViewController添加新視圖來實現相同效果的另一種方法。 如果要屏蔽parentViewController可能具有的其他內容,這將非常有用。

在您的UITableViewController子類中,在-viewDidLoad插入:

//make a cool background image
self.tableView.backgroundColor = [UIColor clearColor];
UIImage *patternImage = [UIImage imageNamed:@"backgroundImage.png"];
UIImageView * backgroundImageView = [[UIImageView alloc] initWithImage:patternImage];

[self.parentViewController.view addSubview:backgroundImageView];
[self.parentViewController.view sendSubviewToBack:backgroundImageView];
//release the background image and image view (the backgroundImageView is still retained by the parentViewController)
[patternImage release];
[backgroundImageView release];

您可能希望跟蹤backgroundImageView,以便刪除或替換它。 上面的代碼示例將它留給parentViewController來管理新視圖。 如果你正在加載和卸載這個UITableView,你將會在每次加載時泄漏這些UIImageViews,除非parentViewController以某種方式以相同的速率釋放它們。

你可以做的是將tableview的backgroundColor屬性設置為[UIColor clearColor],並在TableView下面有一個相同大小和位置的UIImageView來顯示你的背景。

TableView屬性包括背景顏色。 將其設置為clearColor; 並把圖像放在后面。 (在IB中我認為你必須刪除tableview添加圖像,然后再次添加你的tableview)

如果你是UITableViewController的子類,你可以免費獲得一個tableview,不需要ivar或nib,但是因為你使用的是背景我會堅持使用IB。

您可以使用表視圖的backgroundView。 這對我有用。

[self.tableView setBackgroundView: [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"????.png"]]];

哪里????。png是你的背景圖片。

暫無
暫無

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

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