50

UITableViewControllerに背景ビューを設定する方法はありますか?

で使用しているコードを試してみましたUIViewControllerが、ビューはテーブルビューのすべてのコンテンツに適用されます。に背景ビューを追加すると、cellForRowAtIndexPath-methodまったく表示されません。誰かが以前にこれをしたことがありますか、またはそれがどのように行われることができるかについての考えを持っていますか?これが私が使用しているコードです:

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

6 に答える 6

72

長い間経っていることは知っていますが、記録のために..次を使用してより良い解決策を見つけたと思いますUITableView.backgroundView

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

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

iPhoneで320x480の画像サイズで試してみましたが、完璧に動作します(.jpgでも試しました)。

于 2012-02-29T16:46:33.333 に答える
68

(これは基本的に上記の Hans Espen のソリューションと同じですが、簡潔にするために便利な方法を使用しています)

[UITableViewControllerSubclass viewDidLoad]これを -メソッドに入れてください:

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

viewDidLoad メソッドでいくつかの自動解放を回避しても意味がありません。なぜなら、(ビューが実際に読み込まれるとき) めったに呼び出されないため、パフォーマンスへの影響はごくわずかだからです。

注: iPhone では、JPEG やその他の形式ではなく、常に PNG 画像を使用する必要があります。

于 2010-03-17T02:38:22.827 に答える
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];
于 2009-05-22T15:10:52.287 に答える
5

Swiftの場合、これを使用します。

self.tableView.backgroundView = UIImageView(image: UIImage(named: "backgroundImage.png"))
于 2014-12-29T06:51:51.223 に答える
0
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]];
于 2011-09-29T09:02:46.243 に答える
0

セクションを持つ静的UITableViewControllerのC#では、次を使用できます。

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

    }
}

結果は次のようになります。

TableViewController グレーディング背景の設定

于 2018-01-23T05:07:06.967 に答える