iPhone - Setting background on UITableViewController

后端 未结 6 1465
借酒劲吻你
借酒劲吻你 2020-12-04 10:50

Is there any way to set a background view on a UITableViewController?

I try with the code I am using on a UIViewController, but the view comes over all

相关标签:
6条回答
  • 2020-12-04 11:16

    In C# for a static UITableViewController with sections you can use:

    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:

    0 讨论(0)
  • 2020-12-04 11:24

    (This is basically the same as Hans Espen's solution above, but uses convenience methods for brevity)

    Put this in your -[UITableViewControllerSubclass viewDidLoad] method:

    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.

    N.B. You should always use PNG images on the iPhone, not JPEG or any other format.

    0 讨论(0)
  • 2020-12-04 11:28

    For Swift use this,

    self.tableView.backgroundView = UIImageView(image: UIImage(named: "backgroundImage.png"))
    
    0 讨论(0)
  • 2020-12-04 11:32

    I know it's been a long time, but just for the record.. I Think I found a better solution using 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).

    0 讨论(0)
  • 2020-12-04 11:36

    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];
    
    0 讨论(0)
  • 2020-12-04 11:40
    self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]];
    
    0 讨论(0)
提交回复
热议问题