2009-05-22 55 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