2009-05-22 28 views
50

有什麼方法可以在UITableViewController上設置背景視圖?iPhone - 在UITableViewController上設置背景

我嘗試使用我在UIViewController上使用的代碼,但該視圖覆蓋了表視圖的所有內容。如果我在cellForRowAtIndexPath-method中添加背景視圖,則完全不顯示。有沒有人以前做過這個或有一個想法如何做到這一點? 這裏是我正在使用的代碼:

UIImage *image = [UIImage imageNamed: @"background.jpg"]; 
UIImageView *backImage = [[UIImageView alloc] initWithImage: image]; 
[self.view addSubview: backImage]; 
[self.view sendSubviewToBack: backImage]; 

回答

68

(這基本上是與上述相同漢斯·埃斯的解決方案,而是採用了簡潔方便的方法)

在你將這個 - [UITableViewControllerSubclass viewDidLoad]方法:

self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BackgroundPattern.png"]]; 

沒有意義避免了viewDidLoad方法中的一些自動釋放,因爲它只是很少被調用(當視圖實際加載時),因此對性能的影響可以忽略不計。

N.B.您應該始終在iPhone上使用PNG圖像,而不是JPEG或任何其他格式。

+0

爲什麼只有.png for iOS? – 2010-08-19 15:33:15

+13

我應該澄清 - 使用PNG處理所有不是照片的東西。 PNG由iOS設備上的GPU進行硬件加速,因此您的所有UI圖像均應採用PNG格式。 PNG支持alpha透明度,這對於大量的UI圖形來說是必需的。對於任何照片,JPEG壓縮會給你更小的文件大小,所以這可能是一個好處。 – 2010-08-20 03:51:10

+2

雖然這個表設置爲分組樣式時每個組的背景,但有沒有辦法解決這個問題? – 2011-05-30 10:40:57

6

其實,我得到它的工作! :)

NSString *backgroundPath = [[NSBundle mainBundle] pathForResource:@"background" ofType:@"jpg"]; 
UIImage *backgroundImage = [UIImage imageWithContentsOfFile:backgroundPath]; 
UIColor *backgroundColor = [[UIColor alloc] initWithPatternImage:backgroundImage]; 
self.tableView.backgroundColor = backgroundColor; 
[backgroundColor release]; 
0
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]]; 
72

我知道這是一個很長的時間,但僅僅是爲了記錄.. 我想我發現使用UITableView.backgroundView一個更好的解決方案:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lisbon.png"]]; 

self.tableView.backgroundView = imageView; 
[imageView release]; 

我爲320x480 iPhone上的圖像大小嚐試,並完美的作品(我也嘗試過使用.jpg)。

5

對於斯威夫特利用這一點,

self.tableView.backgroundView = UIImageView(image: UIImage(named: "backgroundImage.png")) 
0

C#與部分靜態的UITableViewController你可以使用:

using System; 
using Foundation; 
using UIKit; 
using CoreGraphics; 
using CoreAnimation; 

namespace MyNamespace 
{ 
    public class CustomTableViewController : UITableViewController 
    { 
     public override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 

      SetGradientBackgound(); 
     } 

     private void SetGradientBackgound() 
     { 
      CGColor[] colors = new CGColor[] { 
       UIColor.Purple.CGColor, 
       UIColor.Red.CGColor, 
      }; 

      CAGradientLayer gradientLayer = new CAGradientLayer(); 
      gradientLayer.Frame = this.View.Bounds; 
      gradientLayer.Colors = colors; 
      gradientLayer.StartPoint = new CGPoint(0.0, 0.0); 
      gradientLayer.EndPoint = new CGPoint(1.0, 1.0); 

      UIView bgView = new UIView() 
      { 
       Frame = this.View.Frame 
      }; 
      bgView.Layer.InsertSublayer(gradientLayer, 0); 

      UITableView view = (UITableView)this.View; 
      view.BackgroundColor = UIColor.Clear; 
      view.BackgroundView = bgView; 
     } 

     // Setting cells background transparent 
     public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
     { 
      var cell = base.GetCell(tableView, indexPath); 
      cell.BackgroundColor = UIColor.Clear; 
      return cell; 
     } 

     // Setting sections background transparent 
     public override void WillDisplayHeaderView(UITableView tableView, UIView headerView, nint section) 
     { 
      if (headerView.GetType() == typeof(UITableViewHeaderFooterView)) 
      { 
       UITableViewHeaderFooterView hView = (UITableViewHeaderFooterView)headerView; 
       hView.ContentView.BackgroundColor = UIColor.Clear; 
       hView.BackgroundView.BackgroundColor = UIColor.Clear; 
      } 
     } 

    } 
} 

結果可能是這樣的:

Setting TableViewController grading background