Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method triggered by Change in orientation

Tags:

ios

iphone

Is here a way to run a method when the iOS devices orientation changes?

I would like to change only some objects orientations on the screen, and not others.

What delegates do I use etc.

Cheers -A newbie

like image 690
Sam Jarman Avatar asked Nov 29 '10 07:11

Sam Jarman


People also ask

Which activity method is called when orientation changes occur?

Some device configurations can change during runtime (such as screen orientation, keyboard availability, and when the user enables multi-window mode). When such a change occurs, Android restarts the running Activity ( onDestroy() is called, followed by onCreate() ).

Which method called when orientation changes IOS?

This inherited UIViewController method which can be overridden will be trigger every time the interface orientation will be change.

How do you know if orientation changes?

Detecting Orientation Changes in Javascript Should you need to simply detect when a user changes orientation, you can use the following event listener: screen. orientation. addEventListener("change", function(e) { // Do something on change });

How do you change orientation when retaining data?

Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.


2 Answers

Depends when you want to react:

If before rotation, override from UIViewController:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
 // do something before rotation
}

If you want to perform something after rotation:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
 // do something after rotation
}

Reference:

http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/willRotateToInterfaceOrientation:duration:

like image 51
Brian Liang Avatar answered Oct 01 '22 22:10

Brian Liang


UIDevice posts UIDeviceOrientationDidChangeNotification

UIApplication posts UIApplicationWillChangeStatusBarOrientationNotification and UIApplicationDidChangeStatusBarOrientationNotification and has a related delegate callback for each.

UIViewController receives several orientation related calls triggered by the UIDevice notification if the view controller is part of the controller hierarchy managed by a window.

If you are already using a UIViewController, implement some of the orientation related methods, otherwise register for the UIDevice notifications. The most important UIViewController method is shouldAutorotateToInterfaceOrientation because if that return NO the others are not called.

like image 41
drawnonward Avatar answered Oct 01 '22 22:10

drawnonward