Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a background image in Xcode using swift?

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:

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?

like image 648
GuiGui23 Avatar asked Nov 20 '14 21:11

GuiGui23


People also ask

How do I change the background in Swift?

Find the view or view controller you want to change the background color of. Open it up in the interface builder and open the Attributes Inspector. Find the background color field and set it to the color you want.

How do I add a background image in SwiftUI?

SwiftUI doesn't have a dedicated modifier for displaying background colors or images, but instead lets us specify any kind of background view using its background() modifier. To be clear, you can use any view as your background – another text view if you wanted, for example.

How do I change the background of an app in Xcode?

At the top select the attributes inspector. Under the section "View" there should be a spot that says "Background". click that and choose your colour.


2 Answers

override func viewDidLoad() {     super.viewDidLoad()     self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")) } 
like image 139
Hayley Guillou Avatar answered Oct 19 '22 09:10

Hayley Guillou


I am beginner to iOS development so I would like to share whole info I got in this section.

First from image assets (images.xcassets) create image set .

According to Documentation here 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 background we can call image from image assets by using this method UIImage(named: "background") here is full code example

  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)     } 
like image 32
Mina Fawzy Avatar answered Oct 19 '22 08:10

Mina Fawzy