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

Gui*_*i23 55 background ios swift

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

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.yellowColor()
}
Run Code Online (Sandbox Code Playgroud)

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

Hay*_*lou 105

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

  • 我尝试将其与背景图片一起使用。此解决方案存在一个问题,因为它将图像置于重复模式。 (3认同)

Min*_*wzy 41

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

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

根据文档,这里所有尺寸都需要创建背景图像.

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)
Run Code Online (Sandbox Code Playgroud)

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

  • 很棒的答案!谢谢 ! (3认同)

DàC*_*hún 23

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)

}
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用,但是截至2017年,UIScreen.mainScreen()。bounds现在是UIScreen.main.bounds,而self.view.insertSubview(backgroundImage,atIndex:0)是self.view.insertSubView(backgroundImage,at:0) (2认同)

Ali*_*Ali 16

SWIFT 4

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


小智 11

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

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


小智 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)
Run Code Online (Sandbox Code Playgroud)