ios 如何使用swift在Xcode中设置背景图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27049937/
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
How to set a background image in Xcode using swift?
提问by GuiGui23
How can I set a background image for my main view controller in Xcode 6 using swift? I know that you can do this in the assistant editor as below:
如何使用 swift 在 Xcode 6 中为我的主视图控制器设置背景图像?我知道您可以在助理编辑器中执行此操作,如下所示:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.yellowColor()
}
What is the code to set the background to an image I have in my assets folder?
将背景设置为我的资产文件夹中的图像的代码是什么?
回答by Hayley Guillou
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png"))
}
回答by Mina Fawzy
I am beginner to iOS development so I would like to share whole info I got in this section.
我是 iOS 开发的初学者,所以我想分享我在本节中获得的全部信息。
First from image assets (images.xcassets) create image set .
首先从图像资产(images.xcassets)创建图像集。
According to Documentationhere is all sizes need to create background image.
根据此处的文档,所有尺寸都需要创建背景图像。
For iPhone 5:
640 x 1136
For iPhone 6:
750 x 1334 (@2x) for portrait
1334 x 750 (@2x) for landscape
For iPhone 6 Plus:
1242 x 2208 (@3x) for portrait
2208 x 1242 (@3x) for landscape
iPhone 4s (@2x)
640 x 960
iPad and iPad mini (@2x)
1536 x 2048 (portrait)
2048 x 1536 (landscape)
iPad 2 and iPad mini (@1x)
768 x 1024 (portrait)
1024 x 768 (landscape)
iPad Pro (@2x)
2048 x 2732 (portrait)
2732 x 2048 (landscape)
call the image backgroundwe can call image from image assets by using this method UIImage(named: "background")
here is full code example
调用图像背景我们可以使用此方法从图像资产中调用图像UIImage(named: "background")
这里是完整的代码示例
override func viewDidLoad() {
super.viewDidLoad()
assignbackground()
// Do any additional setup after loading the view.
}
func assignbackground(){
let background = UIImage(named: "background")
var imageView : UIImageView!
imageView = UIImageView(frame: view.bounds)
imageView.contentMode = UIViewContentMode.ScaleAspectFill
imageView.clipsToBounds = true
imageView.image = background
imageView.center = view.center
view.addSubview(imageView)
self.view.sendSubviewToBack(imageView)
}
回答by DàChún
override func viewDidLoad() {
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "bg_image")
backgroundImage.contentMode = UIViewContentMode.scaleAspectfill
self.view.insertSubview(backgroundImage, at: 0)
}
Updated at 20-May-2020:
2020 年 5 月 20 日更新:
The code snippet above doesn't work well after rotating the device. Here is the solution which can make the image stretch according to the screen size(after rotating):
旋转设备后,上面的代码片段无法正常工作。这是可以根据屏幕尺寸(旋转后)使图像拉伸的解决方案:
class ViewController: UIViewController {
var imageView: UIImageView = {
let imageView = UIImageView(frame: .zero)
imageView.image = UIImage(named: "bg_image")
imageView.contentMode = .scaleToFill
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.insertSubview(imageView, at: 0)
NSLayoutConstraint.activate([
imageView.topAnchor.constraint(equalTo: view.topAnchor),
imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
}
回答by Ali
SWIFT 4
快速 4
view.layer.contents = #imageLiteral(resourceName: "webbg").cgImage
回答by Mark Thacker
You can try this as well, which is really a combination of previous answers from other posters here :
您也可以尝试此操作,这实际上是此处其他海报的先前答案的组合:
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "RubberMat")
backgroundImage.contentMode = UIViewContentMode.scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
回答by Iryna Batvina
For Swift 4
对于 Swift 4
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "bg_name.png")
backgroundImage.contentMode = UIViewContentMode.scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
回答by babak
Background Image from APIin swift 4 (with Kingfisher) :
来自swift 4 中API 的背景图像(使用Kingfisher):
import UIKit
import Kingfisher
extension UIView {
func addBackgroundImage(imgUrl: String, placeHolder: String){
let backgroundImage = UIImageView(frame: self.bounds)
backgroundImage.kf.setImage(with: URL(string: imgUrl), placeholder: UIImage(named: placeHolder))
backgroundImage.contentMode = UIViewContentMode.scaleAspectFill
self.insertSubview(backgroundImage, at: 0)
}
}
Usage:
用法:
someview.addBackgroundImage(imgUrl: "yourImgUrl", placeHolder: "placeHolderName")