Search code examples
iosswiftios8ios8.1

Stretch Background for View in Swift


I am using adding Background in iOS Swift App using:

 self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background")!)

However, it does not cover the Screen till bottom. I searched Other questions:

var backImage = UIImage(named: "background")

var resizablebackImage = backImage?.resizableImageWithCapInsets(UIEdgeInsets(top:10,left:0,bottom:10,right:0))

self.view.backgroundColor = UIColor(patternImage:resizablebackImage!)

That does not work. Any ideas?


Solution

  • If your intention is to set it as the background image, don't make image as pattern color and fill it as background color. Instead create an UIImageView set your image as it's image and add it to your UIView.

    var bgImage     = UIImage(named: "background");
    var imageView   = UIImageView(frame: self.view.bounds);
    imageView.image = bgImage
    self.view.addSubview(imageView)
    self.view.sendSubviewToBack(imageView)