使用 Swift 在 xcode 6 中为 ios 应用程序添加背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26446424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding background to ios app in xcode 6 using Swift
提问by user3411711
I am new to ios development so please forgive me. I am taking it step by step and I was wondering how would I make a image I want to be the background in the view controller so I can add on top of it?
我是ios开发的新手,所以请原谅我。我正在一步一步地进行,我想知道如何制作我想作为视图控制器背景的图像,以便我可以在它上面添加?
回答by euthimis87
If you want to do it programmatically using swift here is the code:
如果您想使用 swift 以编程方式执行此操作,则代码如下:
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "yourImage.png"))
Or you can create a uiimage and place your image in it:
或者您可以创建一个 uiimage 并将您的图像放入其中:
let yourImage = UIImage(named: "yourImage.png")
let imageview = UIImageView(image: yourImage)
self.view.addSubview(imageview)
回答by Daniel T.
Add a UIImageView to your view controller's view. Assign your background image to the image view. You can do all this in the storyboard file.
将 UIImageView 添加到您的视图控制器的视图中。将您的背景图像分配给图像视图。您可以在故事板文件中完成所有这些操作。
Good luck!
祝你好运!
回答by DàChún
I prefer to adding a UIImageView and setting it as background.
我更喜欢添加 UIImageView 并将其设置为背景。
override func viewDidLoad() {
let backgroundImage = UIImageView(frame: UIScreen.mainScreen().bounds)
backgroundImage.image = UIImage(named: "bg_image")
self.view.insertSubview(backgroundImage, atIndex: 0)
}
Because I found the following code make the image a little blurry.
因为我发现下面的代码使图像有点模糊。
override func viewDidLoad() {
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "yourImage.png"))
}