How to check the orientation of device programmatically in iPhone? [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
iPhone orientation
How to check the orientation of device at any point off time programmatically in iPhone ?
Thank You.

Here are macros UIDeviceOrientationIsLandscape and UIDeviceOrientationIsPortrait
so rather checking separately you can do it like this ...
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
}
OR
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
}

Try it.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
if ( ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) )
{
// do something for Portrait mode
}
else if(([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) || ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft))
{
// do something for Landscape mode
}

Try also:
UIInterfaceOrientationIsPortrait(orientation)
and
UIInterfaceOrientationIsLandscape(orientation)

-[UIViewController interfaceOrientation]
However it is possible to retrieve the deviceOrientation, independent from the current interface orientation:
[[UIDevice currentDevice] orientation]
Read the documentation for more information though, you need to do some more things to make that work.

You can also check within the function
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
//do for portrait
}
else
{
//do for Landscape
}
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

You should place [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; in your AppDelegate didFinishLaunchingWithOptions
Then, anywhere in your application you can get the current orientation with:
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
And test orientation with:
UIInterfaceOrientationIsPortrait(orientation)
UIInterfaceOrientationIsLandscape(orientation)

Related

Device orientation handling in iOS 6 during view load?

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.

Different Launch orientations for iPad and iPhone?

I'm making a universal app and I was wondering if it is possible to set initial orientation to Landscape for iPad and Portrait for iPhone? Currently, I'm setting initial interface orientation in the info.plist file but it doesn't seem to have different options for iPad and iPhone . If it cannot be done through info.plist file then how to do it programmatically?
programatically you can do using following code -
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return YES;
}
}
else {
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
return YES;
}
}
return NO;
}
It looks like the initial interface orientation property conflicts with the supported orientations. I found the solution here, though.
UIDevice* thisDevice = [UIDevice currentDevice];
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationLandscapeRight];
}
else
{
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}

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]

How to check landscape and portrait mode on appdelegate?

i want to run app in landscape and portrait mode,
How to check landscape and portrait mode on Appdelegate?
I try to call
- (BOOL) isPad{
#ifdef UI_USER_INTERFACE_IDIOM
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#else
return NO;
#endif
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([self isPad]) {
NSLog(#"is pad method call");
return YES;
}
else
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
}
- (BOOL)isPadPortrait
{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
&& (self.interfaceOrientation == UIInterfaceOrientationPortrait
|| self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}
- (BOOL)isPadLandscape
{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
&& (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight
|| self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft));
}
but gives error
Property interface orientation not found on object of type appdelegate.
How can i do that?
You should check the current orientation of your iPad using. I would recommend you to check these conditions on every view and act according to the current orientation:
if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft)
{
//Do what you want in Landscape Left
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
//Do what you want in Landscape Right
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait)
{
//Do what you want in Portrait
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
//Do what you want in Portrait Upside Down
}
I hope this helps you. :)
Feel free to contact me if you require any help.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
NSString *nibName = UIDeviceOrientationIsLandscape(orientation) ? #"Landscape" : #"Portrait";
NSLog(#"orientation is %#",nibName);
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight)
{
NSLog(#"Lanscapse");
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown )
{
NSLog(#"UIDeviceOrientationPortrait");
}
}
The delegate orientation is the orientation that we specify in the plist ,if we not specify in any orientation in plist it will take the portrait mode default .But in delegate you have only the key window that doesn't support the orientation you have to add the viewcontroller or any navigation controller with a view then you can call these function and they will give your the proper orientation for the device.

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.