2015-11-17 96 views

回答

2

下面应该工作:

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

由于功能顾名思义,一个UIColor从您提供的图像创建。

有关更多信息,请参阅Apple文档here

+0

这是有效的,但图像看起来是颠倒的?如果不是变量,该如何更改此图像的布局? – lairdkruger

0

如果您进入故事板,您可以插入图像视图,插入图像,然后在文档大纲中,您可以将图像移动到表格上方,并将其放在它后面。然后在检查器中将tableView alpha设置为.5或者您希望的不透明。

+0

有一点需要注意的是,文字也会改变不透明度。 –

0

有趣的是,我只是做了一个教程,它有这个,但代码被用来代替图像。在雨燕2.0创建文件(姑且称之为BackgroundView)UIView型的,在预发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中的函数。