1

Hello

I'm trying to put an Image as a background for my ViewControllers, guiding myself for other posts I found this way:

I created the following extension:

extension UIView {
    func addBackground() {
        // screen width and height:
        let width = UIScreen.mainScreen().bounds.size.width
        let height = UIScreen.mainScreen().bounds.size.height

        let imageViewBackground = UIImageView(frame: CGRectMake(0, 0, width, height))
        imageViewBackground.image = UIImage(named: "msa_background")

        // you can change the content mode:
        imageViewBackground.contentMode = UIViewContentMode.ScaleAspectFill
        imageViewBackground.clipsToBounds = true

        self.addSubview(imageViewBackground)
        self.sendSubviewToBack(imageViewBackground)
    }
}

I'm calling this extension in every ViewController with the following code:

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.addBackground()
}

The problem is when I rotated the screen the image in the background don't fill all the space in the ViewController, I have checked any possible solution that I found but I can't find a way to do it.

I really appreciate any help from you

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
Jose Raul Perera
  • 778
  • 1
  • 7
  • 35

1 Answers1

1

You are adding the image with the current frame of the screen and never changing it. When you rotate the device it will keep the same frame.

Change it to use AutoLayout like this...

extension UIView {
    func addBackground(imageName: String, contentMode: UIViewContentMode) {
        let imageViewBackground = UIImageView()
        imageViewBackground.image = UIImage(named: imageName)

        // you can change the content mode:
        imageViewBackground.contentMode = contentMode
        imageViewBackground.clipsToBounds = true
        imageViewBackground.translatesAutoresizingMaskIntoConstraints = false

        self.insertSubview(imageViewBackground, atIndex: 0)

        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[imageViewBackground]|", 
                                                                           options: [],
                                                                           metrics: nil,
                                                                           views: ["imageViewBackground", imageViewBackground]))   
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[imageViewBackground]|", 
                                                                           options: [], 
                                                                           metrics: nil, 
                                                                           views: ["imageViewBackground": imageViewBackground]))
    }
}

The auto layout constraints will then make the image view fit to the view no matter how the orientation of the device or frame of the view changes.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • Thank you very much for you help, I'm trying your suggestion right now – Jose Raul Perera Jan 14 '16 at 14:35
  • Thanks again, I think I have problems with the constraints because I'm getting this error:`2016-01-14 09:37:18.720 RapidSentryMaster[3992:61453] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ` – Jose Raul Perera Jan 14 '16 at 14:39
  • @JoseRaulPerera edited to add the property `translatesAutoresizingMaskIntoConstraints` which has to be set to `false`. That should fix the problem. – Fogmeister Jan 14 '16 at 14:40
  • 1
    @Fogmeister you are the best!, thank you so much, it is working perfectly now. – Jose Raul Perera Jan 14 '16 at 14:44