Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Abrexas

macrumors member
Original poster
Jul 29, 2011
52
0
I'm sure this question has been asked and answered several times before, but I am having the hardest time finding a solution.

What I'm trying to do is have the background of my app change when the screens orientation changes. I have two custom images that I have made that I would like to use as the backgrounds, but I just cannot figure out how to get this to work.

Both images have been put into my resources folder, and I know how to set a UIImageView through IB, but to do it programmatically is something I haven't yet learned.

Any suggestions?

Thanks,
- Steven French
 

Abrexas

macrumors member
Original poster
Jul 29, 2011
52
0
Finally figured it out with:

Code:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    
    UIDeviceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation];
    
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        UIImage *img = [UIImage imageNamed:@"DREDNT_port.jpg"];
        [backgroundImage setImage:img];
        
    }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        UIImage *img = [UIImage imageNamed:@"Pledge_land.jpg"];
        [backgroundImage setImage:img];
    }
    
}

This creates a slight lag when changing, but that is not my worry. If anyone knows of a better solution than I would love to lear it. ^_^
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,563
6,062
If you move your code into:
Code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

you might be able to remove the lag.
 

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
You also might not want to use JPG's.. In any case, PNG's are the way to go for iOS development, for the simple reason of packaging, loading because of the properties of PNG's, they might be a big bigger, but in the end, it's the best!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.