在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;
}
}
}
}
结果可能是这样的: