如何在swift中将背景图像设置为colorWithPatternImage

Mar*_*oun 44 uiview swift

我的问题是,是否有可能让UIView显示图像,如果有,我怎么做呢?我所能找到的是colorwithpatternImage不再适用于swift的东西.

我现在尝试使用的代码是:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];  
Run Code Online (Sandbox Code Playgroud)

提前感谢您的回答!

cod*_*ter 118

请参阅UIColor文档.

在Swift中,您必须调用便捷初始化程序.这是因为在Swift中,所有返回其类实例的Objective-C类方法都成为了方便的初始化器.

以下是它在Swift中的外观:

 self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png"))
Run Code Online (Sandbox Code Playgroud)

+ (UIColor *)colorWithPatternImage:(UIImage *)image返回一个UIColor实例,因此它将成为Swift中的一个便利初始化器.同样,UIImage imageNamed:成为init(patternImage image: UIImage!).


DàC*_*hún 7

我更喜欢这个:

let backgroundImage = UIImageView(frame: UIScreen.mainScreen().bounds)
backgroundImage.image = UIImage(named: "background.png")
self.view.insertSubview(backgroundImage, atIndex: 0)
Run Code Online (Sandbox Code Playgroud)

由于将图像设置为backgroundColor会使其模糊不清.