如何使用swift在Xcode中设置背景图像?

时间:2014-11-20 21:31:42

标签: ios swift background

如何使用swift为Xcode 6中的主视图控制器设置背景图像?我知道您可以在助理编辑器中执行此操作,如下所示:

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.yellowColor()
}

将背景设置为我的资源文件夹中的图像的代码是什么?

10 个答案:

答案 0 :(得分:95)

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png"))
}

答案 1 :(得分:39)

我是iOS开发的初学者,所以我想分享我在本节中获得的全部信息。

首先从图像资源(images.xcassets)创建图像集。

根据Documentation,所有尺寸都需要创建背景图像。

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)

调用图像背景我们可以使用此方法从图像资源调用图像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)
    }

答案 2 :(得分:20)

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)

}

答案 3 :(得分:11)

您也可以尝试这一点,这实际上是此处其他海报的先前答案的组合:

    let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
    backgroundImage.image = UIImage(named: "RubberMat")
    backgroundImage.contentMode =  UIViewContentMode.scaleAspectFill
    self.view.insertSubview(backgroundImage, at: 0)

答案 4 :(得分:10)

SWIFT 4

view.layer.contents = #imageLiteral(resourceName: "webbg").cgImage

答案 5 :(得分:8)

适用于Swift 4

[SC] StartService FAILED with error 129.

答案 6 :(得分:1)

来自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)

}
}

用法:

someview.addBackgroundImage(imgUrl: "yourImgUrl", placeHolder: "placeHolderName")

答案 7 :(得分:1)

TableViewController 的 SWIFT 5

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)

tableView.backgroundView = UIImageView(image:"image.jpg")
tableView.backgroundView?.contentMode = .scaleToFill
}

答案 8 :(得分:0)

XCode 12.2游乐场中的Swift 5.3

将光标放置在#insertLiteral中早期版本中的代码中。从顶部菜单Editor->Insert Image Literal...中选择并导航到文件。点击Open

该文件已添加到游乐场资源文件夹中,然后在运行游乐场时,无论选择了放置在哪个视图中,它都会出现。

如果文件已在游乐场捆绑包中,例如在/ Resources中,可以将其直接拖动到代码中所需的位置(将在其中显示一个图标)。

cf。 Apple help docs give details of this and how to place other colour and file literals.

答案 9 :(得分:-1)

在Swift 4.2和Xcode 10中

import UIKit
class SharedClass: NSObject {
    //Shared class instance
    static let sharedInstance = SharedClass()

    //Add background image for view's
    func backgroundImage(view: UIView) {
        let imageView = UIImageView(image: UIImage(named: "iphonebackground")!)//Your image name here
    imageView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)
        view.insertSubview(imageView, at: 0)//To set first of all views in VC
    }

    private override init() {

    }

}

现在在这样的视图控制器中调用此函数

SharedClass.sharedInstance.backgroundImage(view: self.view)//This is your view