【问题标题】:iPhone - Setting background on UITableViewControlleriPhone - 在 UITableViewController 上设置背景
【发布时间】:2009-05-22 15:04:26
【问题描述】:

有没有办法在 UITableViewController 上设置背景视图?

我尝试使用在UIViewController 上使用的代码,但视图会覆盖表视图的所有内容。如果我在cellForRowAtIndexPath-method 中添加背景视图,它根本不会显示。有没有人这样做过或知道如何做到这一点? 这是我正在使用的代码:

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

【问题讨论】:

    标签: iphone background uitableview


    【解决方案1】:

    我知道这已经很久了,但只是为了记录.. 我想我使用UITableView.backgroundView 找到了更好的解决方案:

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

    我尝试在 iPhone 上使用 320x480 的图像尺寸,效果完美(我也尝试使用 .jpg)。

    【讨论】:

    • 这样更好,也放在标题后面
    • 同意,在使用颜色模式的解决方案上使用了这个解决方案。
    • @MárioCarvalho ainda bem que funcionou! ;)
    • 此方法有效,但它将背景插入到 ViewController,而不是我需要在 TableView 的内容中添加背景(我不希望将背景插入到标题或TableView 的页脚)
    【解决方案2】:

    (这与上面 Hans Espen 的解决方案基本相同,但为简洁起见使用了方便的方法)

    把它放在你的 -[UITableViewControllerSubclass viewDidLoad] 方法中:

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

    在 viewDidLoad 方法中避免几次自动释放是没有意义的,因为它很少被调用(当视图实际加载时),因此对性能的影响可以忽略不计。

    注意您应该始终在 iPhone 上使用 PNG 图像,而不是 JPEG 或任何其他格式。

    【讨论】:

    • 我应该澄清一下 - 对所有非摄影作品使用 PNG。 PNG 是由 iOS 设备上的 GPU 进行硬件加速的,因此您的所有 UI 图像都应该是 PNG 格式。 PNG 支持 alpha 透明度,这是很多 UI 图形的必需品。对于任何摄影作品,JPEG 压缩将为您提供更小的文件大小,因此这可能是一个好处。
    • 这会在表格设置为分组样式时设置每个组的背景,但是有没有办法解决这个问题?
    • 从 iOS 3.2 开始,有一个 UITableView.backgroundView 属性,AFAIK 使颜色/图案变通方法变得多余(如果您可以放弃 3.2 之前的支持)。
    • 我们在哪里可以找到使用 UITableView.backgroundView 的这个问题的答案?如果您添加新答案并标记我,我会投票。
    • initWithNibName 不是神,需要在 didLoad 或 willAppear 上出现!大神解答
    【解决方案3】:

    实际上,我成功了! :)

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

    【讨论】:

      【解决方案4】:

      对于 Swift 使用这个,

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

      【讨论】:

      • 这个解决方案对我有用,但我发现,旧版本不接受 xcode 8 中 opaque、bgcolor... 的属性(例如 iOS 9)。我不知道,如果我错过了什么,但我构建了一个解决方案,将 tableviewcells 引用为代码的出口,并使它们不透明,而 backgroundview 为零。也许这会帮助别人:)
      【解决方案5】:
      self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]];
      

      【讨论】:

        【解决方案6】:

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

        结果可能是这样的:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-09-27
          • 1970-01-01
          • 1970-01-01
          • 2012-04-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多