web-dev-qa-db-fra.com

iPhone - Configuration de l'arrière-plan sur UITableViewController

Est-il possible de définir une vue en arrière-plan sur un UITableViewController?

J'essaie avec le code que j'utilise sur une UIViewController, mais la vue recouvre tout le contenu de la vue table. Si j'ajoute la vue d'arrière-plan dans le cellForRowAtIndexPath-method, elle ne s'affiche pas du tout. Quelqu'un a-t-il déjà fait cela auparavant ou a-t-il une idée de la façon dont cela peut être fait?

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

(Ceci est fondamentalement identique à la solution de Hans Espen ci-dessus, mais utilise des méthodes pratiques pour plus de concision)

Mettez ceci dans votre méthode -[UITableViewControllerSubclass viewDidLoad]:

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

Il est inutile d'éviter quelques auto-relâchements dans une méthode viewDidLoad, car celle-ci n'est appelée que rarement (lorsque la vue se charge réellement) et aura donc un impact négligeable sur les performances.

N.B. Vous devez toujours utiliser des images PNG sur l'iPhone, pas au format JPEG ou tout autre format.

68
Nick Forge

Je sais que cela fait longtemps, mais pour mémoire ... je pense avoir trouvé une meilleure solution avec UITableView.backgroundView:

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

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

J'ai essayé avec une taille d'image de 320x480 sur iPhone, et fonctionne parfaitement (j'ai essayé avec .jpg également).

72
Frade

En fait, ça marche! :)

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];
6
Hans Espen

Pour Swift utilisez ceci,

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

Dans C # pour un UITableViewController statique avec des sections, vous pouvez utiliser:

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

    }
}

Le résultat peut être quelque chose comme ceci:

 Setting TableViewController grading background

0
gts
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]];
0
Sandip Patel - SM