2014-11-20 17 views
23

Come posso impostare un'immagine di sfondo per il mio controller di visualizzazione principale in Xcode 6 usando swift? So che si può fare questo in assistente al montaggio, come di seguito:Come impostare un'immagine di sfondo in Xcode usando swift?

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

Qual è il codice per impostare lo sfondo a un'immagine che ho nella mia cartella Assets?

+0

È alla ricerca di [UIColor.colorWithPatternImage] (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIColor_Class/index.html#// apple_ref/OCC/CLM/UIColor/colorWithPatternImage :)? Maggiori informazioni sull'immagine di sfondo per UIView [qui] (http://beageek.biz/how-to-set-background-image-uiview/) – Xenyal

risposta

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

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

} 
+0

Questo ha funzionato per me, tuttavia a partire dal 2017, UIScreen.mainScreen(). è ora UIScreen.main.bounds e self.view.insertSubview (backgroundImage, atIndex: 0) è self.view.insertSubView (backgroundImage, a: 0) – jffgrdnr

28

io sono principiante allo sviluppo iOS quindi vorrei condividere tutta l'informazioni che ho avuto in questa sezione.

Prima dalle risorse immagine (images.xcassets) creare il set di immagini.

Secondo Documentation qui è tutte le dimensioni devono creare immagine di sfondo.

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) 

chiamata l'immagine sfondo possiamo chiamare immagine da attività di immagine utilizzando questo metodo UIImage(named: "background") qui è il codice completo esempio

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) 
    } 
+1

Ottima risposta! Grazie ! – Zl3n

11

Si può provare questo pure, che è in realtà una combinazione di risposte precedenti da altri manifesti qui:

let backgroundImage = UIImageView(frame: UIScreen.main.bounds) 
    backgroundImage.image = UIImage(named: "RubberMat") 
    backgroundImage.contentMode = UIViewContentMode.scaleAspectFill 
    self.view.insertSubview(backgroundImage, at: 0) 
1
view.layer.contents = #imageLiteral(resourceName: "webbg").cgImage 
3

Per 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) 
Problemi correlati