orientation changed is not working
-
12-12-2019 - |
Question
Thanks in advance
I want to detect when orientation changed in my device ... for that I used this listener
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
and orientation changed method is
- (void) orientationChanged:(id)obj
{
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight )
{
self.view=self.landscapeView;
NSLog(@"landscapeView");
//[self loadView1];
}
else
{
self.view = self.portraitView;
NSLog(@"portraitView");
//[self loadView1];
}
}
and every time its went into that orientation which was on ViewDidLoad() methode ...
can any one help me..
Solution
Try this
[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
if ( ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) )
{
//code here
}
OTHER TIPS
Your question was vert vague , This is a general explanation. You can override below mentioned methods to handle orientation changes
Over ride below mentioned methods
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// return YES to supported orientation.
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
/* Subclasses may override this method to perform additional actions immediately
prior to the rotation. For example, you might use this method to disable view
interactions, stop media playback, or temporarily turn off expensive drawing or live
updates. You might also use it to swap the current view for one that reflects the new
interface orientation. When this method is called, the interfaceOrientation property
still contains the view’s original orientation.
*/
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
/*Subclasses may override this method to perform additional actions immediately after
the rotation. For example, you might use this method to reenable view interactions,
start media playback again, or turn on expensive drawing or live updates. By the time
this method is called, the interfaceOrientation property is already set to the new
orientation.*/
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
/*This method is called from within the animation block used to rotate the view.
You can override this method and use it to configure additional animations that should
occur during the view rotation. For example, you could use it to adjust the zoom level
of your content, change the scroller position, or modify other animatable properties
of your view.*/
}
Don't check the device orientation in viewDidLoad or viewDidAppear like this
if( [UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft)
OR if(UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation))
Because many times it will give un-expected results, like if the iPhone/iPad is placed on top of a table (I had experienced this...Very Nasty Issue).
This apple developer link will give more info about handling orientations
change the following
- (void) orientationChanged:(id)obj
{
if( [UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight )
{
self.view=self.landscapeView;
NSLog(@"landscapeView");
//[self loadView1];
}
else
{
self.view = self.portraitView;
NSLog(@"portraitView");
//[self loadView1];
}
}
why don't you just use shouldAutorotateToInterfaceOrientation
?
it's the method in UIViewController that is already invoked...