uitableview 后面的背景图像视图

Background image view behind a uitableview

是否可以在UITableView后面加一个imageView?我将如何添加这张图片:

以便用户可以在 tableview 后面看到它(背景设置为清除)?

以下应该有效:

tableView.backgroundColor = UIColor(patternImage: UIImage(named: "your_image_name")!)

正如函数名称所暗示的那样,UIColor 是根据您提供的图像创建的。

有关详细信息,请参阅 Apple 文档 here

如果你进入情节提要,你可以插入图像视图,插入图像,然后在文档大纲中,你可以将图像移到 table 上方,它会移到它后面。然后将检查器中的 tableView alpha 设置为 .5 或您想要的任何不透明。

有趣的是,我刚刚做了一个教程,里面有这个,但是使用了代码而不是图像。在 Swift 2.0 中,您创建一个 UIView 类型的文件(我们称它为 BackgroundView),将此代码添加到预制的 drawRect 函数中:

// Change these colors to the ones you want.
let lightPurple: UIColor = UIColor(red: 0.377, green: 0.075, blue: 0.778, alpha: 1.000)
let darkPurple: UIColor = UIColor(red: 0.060, green: 0.036, blue: 0.202, alpha: 1.000)

let context = UIGraphicsGetCurrentContext()

//// Gradient Declarations
let purpleGradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), [lightPurple.CGColor, darkPurple.CGColor], [0, 1])

//// Background Drawing
let backgroundPath = UIBezierPath(rect: CGRectMake(0, 0, self.frame.width, self.frame.height))
CGContextSaveGState(context)
backgroundPath.addClip()
CGContextDrawLinearGradient(context, purpleGradient,
    CGPointMake(160, 0),
    CGPointMake(160, 568),
    [.DrawsBeforeStartLocation, .DrawsAfterEndLocation])

然后在TableViewController中创建一个configureView函数,将这行代码放入其中:

tableView.backgroundView = BackgroundView()

然后调用viewDidLoad中的函数。