如何在swift中将背景图像设置为colorWithPatternImage [英] how to set a background image as colorWithPatternImage in swift
问题描述
我的问题是,是否可以让 UIView 显示图像,如果可以,我该怎么做?我能找到的只是 colorwithpatternImage
不再适用于 swift .
My questions is, is it possible to have a UIView display an image, and if so, how so I do it? All I can find is the colorwithpatternImage
which no longer works with swift.
我现在尝试使用的代码是:
The code I am trying to use now is:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
预先感谢您的回答!
推荐答案
参考UIColor
文档.
在 Swift 中,您必须调用便利构造函数.这是因为在 Swift 中,所有返回其类实例的 Objective-C 类方法都变成了便利初始化器.
In Swift, you have to call a convenience initializer. This is because in Swift, all Objective-C class methods which return an instance of their class become convenience initializers.
这是它在 Swift 中的样子:
Here's how it looks in Swift:
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png"))
+ (UIColor *)colorWithPatternImage:(UIImage *)image
返回一个 UIColor
实例,因此它将成为 Swift 中的便利初始化程序.同样,UIImage imageNamed:
变成了 init(patternImage image: UIImage!)
.
+ (UIColor *)colorWithPatternImage:(UIImage *)image
returns a UIColor
instance, so it will become a convenience initializer in Swift. Similarly, UIImage imageNamed:
becomes init(patternImage image: UIImage!)
.
这篇关于如何在swift中将背景图像设置为colorWithPatternImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!