iPhone - Setting background on UITableViewController

后端 未结 6 1549
借酒劲吻你
借酒劲吻你 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:

提交回复
热议问题