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

问题描述 投票:46回答:8

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

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

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

ios swift background
8个回答
97
投票
override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png"))
}

40
投票

我是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)
    }

21
投票
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)

}

11
投票

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

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

11
投票

SWIFT 4

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

8
投票

对于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)

1
投票

来自API的背景图片在swift 4(与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")

-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
© www.soinside.com 2019 - 2024. All rights reserved.