简体   繁体   English

iPhone - 在UITableViewController上设置背景

[英]iPhone - Setting background on UITableViewController

Is there any way to set a background view on a UITableViewController? 有没有办法在UITableViewController上设置背景视图?

I try with the code I am using on a UIViewController , but the view comes over all the contents of the table view. 我尝试使用我在UIViewController上使用的代码,但视图覆盖​​了表视图的所有内容。 If I add the background view in the cellForRowAtIndexPath-method , it is not showing at all. 如果我在cellForRowAtIndexPath-method添加背景视图,则根本不显示。 Has anyone done this before or have an idea on how it can be done? 有没有人以前做过这个或者知道如何做到这一点? Here is the code I am using: 这是我正在使用的代码:

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

I know it's been a long time, but just for the record.. I Think I found a better solution using UITableView.backgroundView : 我知道这已经很长时间了,但仅仅是为了记录......我想我找到了一个更好的解决方案,使用UITableView.backgroundView

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

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

I tried with an image size of 320x480 on iPhone, and works perfect (I have tried with .jpg also). 我尝试在iPhone上使用320x480的图像尺寸,并且工作完美(我也尝试过使用.jpg)。

(This is basically the same as Hans Espen's solution above, but uses convenience methods for brevity) (这与Hans Espen上面的解决方案基本相同,但为了简洁起见使用便利方法)

Put this in your - [UITableViewControllerSubclass viewDidLoad] method: 把它放在你的 - [UITableViewControllerSubclass viewDidLoad]方法中:

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

There's no point in avoiding a couple of autoreleases in a viewDidLoad method, since it only gets called rarely (when the view actually loads) and will therefore have negligible impact on performance. 避免在viewDidLoad方法中使用几个自动释放是没有意义的,因为它很少被调用(当视图实际加载时),因此对性能的影响可以忽略不计。

NB You should always use PNG images on the iPhone, not JPEG or any other format. 注意您应始终在iPhone上使用PNG图像,而不是JPEG或任何其他格式。

Actually, I got it working! 实际上,我让它工作了! :) :)

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

对于Swift使用这个,

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

In C# for a static UITableViewController with sections you can use: 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;
            }
        }

    }
}

The result can be something like this: 结果可能是这样的:

设置TableViewController评分背景

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM