Device orientation handling in iOS 6 during view load? - iphone

I have a Tab bar application. I was using XCode 4.3.3. I have upgraded to 4.5.2 with iOS6 stuffs.
My code in the shouldAutorotateToInterfaceOrientation for each view will check the current device orientation and place all the UI components properly.
But after upgrading to iOS 6, this code is not executing. I have tried for almost one day to fix this, but had no luck.
I have also tried
UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
in viewLoad, viewDidLoad, viewWillAppear
What should I do if I want to check the orientation during the view load and place the UI components to appropriate places during view load. Kindly give your suggestion.
Thanks

if you want to check which is current orientation, then you just write in prifix.pch below line
#define isPortrait [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown
then where you want to check orientation ,write condition like below
if(isPortrait)
{
//portrait mode....
}
else
{
//landscape mode....
}
let me know it is working or not...
Happy coding!

To check orientation in ios6,you will have to write below methods and also in your info.plist you will have to define which orientation you want...
let me know whether it is working or not!!!
Happy Coding!!!!
- (BOOL)shouldAutorotate{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;//you can write here which ever orientation you want..
}

you manage Oriantation like this ways:-
-(BOOL)shouldAutorotate
{
return YES;
}
and check every time in ViewWillApear device Oriantation like:-
- (void)willRotateToOrientation:(UIInterfaceOrientation)newOrientation {
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
if (newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight) {
//set your landscap View Frame
[self supportedInterfaceOrientations];
}
}
else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
if(newOrientation == UIInterfaceOrientationPortrait || newOrientation == UIInterfaceOrientationPortraitUpsideDown){
//set your Potrait View Frame
[self supportedInterfaceOrientations];
}
}
// Handle rotation
}
-(void)viewWillAppear:(BOOL)animated
{
[self willRotateToOrientation:[[UIDevice currentDevice] orientation]];
[super viewWillAppear:YES];
}
UPDATE
likely people use checking deviceorientation like below way in putting this line in to ViewWillApear:-
[[UIApplication sharedApplication] statusBarOrientation];
[[UIDevice currentDevice] orientation];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];
and
-(void)deviceRotated:(NSNotification*)notification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//Do your stuff for landscap
}
else if(orientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
//Do your stuff for potrait
}
}

At viewDidLoad method you need to check for the same and accordingly you need to write code unless it may affect view if it not in on portrait.

Related

Issue with auto rotation

I am having a MPMoviePlayerView that i am creating programatically.My PlayerView auto rotation works fine with iOS 5 but it do not work in iOS 6 and iOS 7.I had tried adding orientation detection but problem remain same.
Can anyone tell me how can i support auto rotation in iOS 5 ,iOS 6 and iOS 7.
Thanks ,
Try to implement this method in AppDelegate:
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
}
you can make condition here for both mode.
such as if media player is in full screen then
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
otherwise return UIInterfaceOrientationMaskPortrait;
thanks
[[UIApplication sharedApplication] statusBarOrientation];
[[UIDevice currentDevice] orientation];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceRotatedEventList:) name:UIDeviceOrientationDidChangeNotification object:nil];
-(void)deviceRotatedEventList:(NSNotification*)notification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//Do your stuff for landscap
}
else if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
}
}
//For rotation method write ur code potraite and landscape mode whatever........

Orientation issues in Ios 7?

Hi I am working on iphone app (not for ipad) which contains orientation in both protrait and landscape. My problem is sometimes orientation is not done. If i turn device to landscape mode, it is stuck in that mode only, not turn to protrait mode and vice versa.
I dont know the problem. i activated all device orientation modes in deployment info section.
Can anyone tell me the solution for this.
Thanks in advance,
Mahesh.
-(void)viewWillAppear:(BOOL)animated
{
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown)
{
// Write your orientation code here.
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft||interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
// Write your orientation code here.
}
}
// For ios5
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown)
{
// Write orientation code here for ios5
}
else if (interfaceOrientation ==UIInterfaceOrientationLandscapeLeft||interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
// Write orientation code here for ios5
}
return UIInterfaceOrientationMaskAll;
}
// Write code for ios6 and above
-(BOOL)shouldAutorotate
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
return YES;
}
else
return NO;
}
-(void)viewWillLayoutSubviews
{
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown)
{
// Write orientation code here for ios5
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft||interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
// Write orientation code here for ios5
}
}

Rotation Code not working in iOS6 it worked perfectly on iOS 5

I have this code and it works perfectly on iOS 5 but not on iOS 6 please help
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
return (orientation != UIDeviceOrientationPortrait) &&
(orientation != UIDeviceOrientationPortraitUpsideDown);
}
let try this :
First inside viewDidLoad function use this code :
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
then, i'm create function that will receive notification for two lines of code :
- (void)orientationChanged:(NSNotification *)object{
NSLog(#"orientation change");
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation == UIInterfaceOrientationPortraitUpsideDown){
if(deviceOrientation ==UIInterfaceOrientationPortrait){
NSLog(#"Changed Orientation To Portrait");
// Handle your view.
}
}else{
if(deviceOrientation ==UIInterfaceOrientationLandscapeLeft){
NSLog(#"Changed Orientation To Landscape left");
// Handle your view.
}else{
NSLog(#"Changed Orientation To Landscape right");
// Handle your view.
}
}
}
i hope my answer will help. Cheers.
In iOS6, "shouldAutorotateToInterfaceOrientation" method is deprecated.Try setting orientation in plist file.

iPad orientation not being returned correctly

I'm basically running this code:
UIInterfaceOrientation statusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(statusBarOrientation == UIInterfaceOrientationPortrait){
NSLog(#"orientation is portrait");
}
However, regardless of the actual orientation in the simulator or my iPad, it is printing "orientation is portrait". Trying to NSLog the statusBarOrientation as a %d also returns 1 no matter what the orientation.
I've stuck this is my app delegate, my view controller, and the class that I need it in, and its the same thing. All 4 device orientations are supported in my info.plist / target settings.
Does anyone have a sure fire way of figuring out the interface orientation, or why mine is not working? Thanks
if you dont like to used Notification for orientation.. Then use below method too.
this is example of Only Landscape Orientation in iPad and Portrait in iPhone...
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
if(interfaceOrientation==UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation==UIInterfaceOrientationLandscapeLeft)
return YES;
else
return NO;
}
else
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}
You can REGISTER FOR notifications on orientation changes:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:#"UIDeviceOrientationDidChangeNotification"
object:nil];
- (void)didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if ((orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)) {
// DO STUFF
}
else if (orientation == UIDeviceOrientationPortrait) {
//DO MORE STUFF
}
}
Currently you are returning the orientation of the status bar. Instead get the orientation of the device: [[UIDevice currentDevice] orientation]

About the orientation of iPhone

How to get the current orientation of iPhone?
I surfed this site, and found two methods as followings.
[UIApplication sharedApplication].statusBarOrientation;
[[UIDevice currentDevice] orientation];
Which one is the right way to get the current orientation ?
I tried two methods under simulator 4.1, but there are some problems for both methods.
Register your class to listen to UIDeviceOrientationDidChangeNotification then handle the device orientation accordingly.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(deviceRotated:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
and handle the device's orientation properly:
- (void)deviceRotated: (id) sender{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationFaceUp ||
orientation == UIDeviceOrientationFaceDown)
{
//Device rotated up/down
}
if (orientation == UIDeviceOrientationPortraitUpsideDown)
{
}
else if (orientation == UIDeviceOrientationLandscapeLeft)
{
}
else if (orientation == UIDeviceOrientationLandscapeRight)
{
}
}
[[UIDevice currentDevice] orientation] gets the current physical orientation of the device. [UIApplication sharedApplication].statusBarOrientation gets the orientation of the UI. If the app ever returns NO to the shouldAutorotateToInterfaceOrientation: method, the two values will not be the same.