4

I have two images bg-land and bg-port for different orientation. I wanna change the image on chnaging orientation. But, the image does not change. When I load the Simulator in LandScape, bg-land works perfect but does not work with Rotation. Code:

   override func viewDidLoad() {
        super.viewDidLoad()
        loadBackground()  
    }

    func loadBackground(){

        if(UIApplication.sharedApplication().statusBarOrientation.isPortrait){
            var bgImage      = UIImage(named: "bg-port")
            var background   = UIImageView(frame: self.view.bounds);
            background.image = bgImage
            self.view.addSubview(background)
            self.view.sendSubviewToBack(background)
            println(bgImage)
        } else {
            var bgImage      = UIImage(named: "bg-land")
            var background   = UIImageView(frame: self.view.bounds);
            background.image = bgImage
            self.view.addSubview(background)
            self.view.sendSubviewToBack(background)
            println(bgImage)
        }


    }

    override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
        loadBackground()
    }
tika
  • 7,135
  • 3
  • 51
  • 82

2 Answers2

2

This code works fine for me try this:

   override func viewDidLoad() {
   super.viewDidLoad()
   self.loadBackground()
}
func loadBackground(){

    if(UIApplication.sharedApplication().statusBarOrientation.isPortrait){
        let imageName = "Texture.jpg"
        let image = UIImage(named: imageName)
        let imageView = UIImageView(image: image!)

        imageView.frame = self.view.bounds
        view.addSubview(imageView)
    } else {
        let imageName = "AppIcon.png"
        let image = UIImage(named: imageName)
        let imageView = UIImageView(image: image!)

        imageView.frame = self.view.bounds
        view.addSubview(imageView)
    }


}
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
    loadBackground()
}

Don't forget to change image name from this code.

May be this will help you.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • 2
    A suggestion. N imageView objects will be added to the screen if the user rotates the device N times. – Varun Singh Jan 10 '15 at 07:21
  • Another problem is if there is anything in the storyboard file, your imageview will cover those things, as you are adding it on top of that – Midhun MP Jan 10 '15 at 07:41
1

It's not changing because you are sending the newly added UIImageView to the back of your previously created UIImageView. Instead of doing that, create a member variable in your class for UIImageView. (Name it as background)

func loadBackground()
{
   if self.background == nil
   {
      self.background = UIImageView()
      self.view.addSubview(self.background)
      self.view.sendSubviewToBack(self.background)
   }

   if(UIApplication.sharedApplication().statusBarOrientation.isPortrait)
   {
        var bgImage           = UIImage(named: "bg-port")
        self.background.frame = self.view.bounds;
        self.background.image = bgImage
   }
   else
   {
        var bgImage           = UIImage(named: "bg-land")
        self.background.frame = self.view.bounds;
        background.image      = bgImage
   }
}

But it is better to add a UIImageView outlet to your interface and loading the image using code, rather than creating one at run-time. And I prefer that would be a nice approach.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • it says self.background is not accepted. `ViewController doesnot have a member named background` – tika Jan 10 '15 at 07:27
  • @user2078462: I mentioned in my answer that, you need to create a UIImageView as your class member variable and to name it as background – Midhun MP Jan 10 '15 at 07:27
  • @user2078462: I didn't get you. You mean is there a way without doing it by code ? Yes. You can use the storyboard and auto-layout feature for this (For loading different image, you need to do that by code) – Midhun MP Jan 10 '15 at 07:37