Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing UIView on orientation change

Hey all. I have a fairly simple question. I am developing a "rich" iPad app, and I have two background images specifically designed for landscape and portrait. I'd like this ImageView to automatically change depending on the devices orientation. (like pretty much all of Apples iPad apps).

Can anyone point me in the right direction? I'm assuming it would be something I do on viewDidLoad..

like image 359
Designeveloper Avatar asked Apr 29 '10 03:04

Designeveloper


People also ask

How do I change the orientation of the device using JavaScript?

You can use the orientationchange event in an addEventListener method: window.addEventListener("orientationchange", function(event) { console.log("the orientation of the device is now " + event. target. screen. orientation. angle); }); Or use the onorientationchange event handler property:

Should I avoid using the orientationchange event?

Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time. The orientationchange event is fired when the orientation of the device has changed.

When does the orientationchange event fire?

Be aware that this feature may cease to work at any time. The orientationchange event is fired when the orientation of the device has changed. This event is deprecated.


1 Answers

The best thing you can do is to change the frames of your subview frames according to your interface orientations. You can do it like:

 #pragma mark -
 #pragma mark InterfaceOrientationMethods

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (UIInterfaceOrientationIsPortrait(interfaceOrientation) || UIInterfaceOrientationIsLandscape(interfaceOrientation));
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
        //self.view = portraitView;
        [self changeTheViewToPortrait:YES andDuration:duration];

    }
    else if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
        //self.view = landscapeView;
        [self changeTheViewToPortrait:NO andDuration:duration];
    }
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];

    if(portrait){
        //change the view and subview frames for the portrait view
    }
    else{   
        //change the view and subview  frames for the landscape view
    }

    [UIView commitAnimations];
}

Hope this helps.

like image 151
Madhup Singh Yadav Avatar answered Oct 25 '22 10:10

Madhup Singh Yadav