How to get the devices UIInterfaceOrientation - ios

I am trying to capture when the UIInterfaceOrientation changes. I know how to do it with UIDeviceOrientation but I am wanting to prevent anything but landscape left/right and portrait.
I was using UIDeviceOrientation but every time I laid it face up everything would go crazy on my app.
So I was wanting to know how to do something like this
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
if (interfaceOrientation landscape) {
if (orientation == UIDeviceOrientationLandscapeLeft)
{
}
else if (orientation == UIDeviceOrientationLandscapeRight)
{
}
}
if (interfaceOrientation Portrait) {
}
so I only look at either landscape or portrait.
any help would be greatly appreciated.

if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
}
It's a C function, not an objective C function.
UIInterfaceOrientation is an enum.
The other option would be:
if (interfaceOrientation & (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight)) {
}
From the UIApplication.h header:
// Note that UIInterfaceOrientationLandscapeLeft is equal to UIDeviceOrientationLandscapeRight (and vice versa).
// This is because rotating the device to the left requires rotating the content to the right.
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
};
/* This exception is raised if supportedInterfaceOrientations returns 0, or if preferredInterfaceOrientationForPresentation
returns an orientation that is not supported.
*/
UIKIT_EXTERN NSString *const UIApplicationInvalidInterfaceOrientationException NS_AVAILABLE_IOS(6_0);
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
};

Related

Xcode 6 iOS 8 Rotation Issue - Large Blank Space On Screen

I am running into a very strange issue with my iPad app in Xcode 6. Previously (When building with Xcode 5 / iOS 7 SDK) it would handle rotations with no problem, but now that developers are required to build with Xcode 6 / iOS 8 SDK, my app no longer handles rotation properly.
I did some research and was able to use the viewWillTransitionToSize method in order to get all my subviews to properly rotate and resize themselves. However, my screen has a large white rectangle along the side when I rotate. If I start the app in portrait and rotate to landscape, it goes from looking normal to having a large white space on the right.
(The same thing happens if I start in landscape and rotate to portrait, but the white space is on the bottom).
My subviews are definitely resizing themselves properly, but that white space unfortunately covers them up. I checked the both the bounds and frame of my UIScreen window and those values seem valid (1024w x 768h in landscape, 768w x 1024h in portrait). Previously it was a black space but once I added the line [self.window setFrame:[[UIScreen mainScreen] bounds]]; to my didFinishLaunchingWithOptions method, it became a white space.
The following is the viewWillTransitionToSize code that I'm using:
- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size WithTransitionCoordinator:coordinator];
UIInterfaceOrientation *orientation = [self interfaceFromTransform:[coordinator targetTransform]];
UIInterfaceOrientation *oldOrientation = [[UIApplication sharedApplication] statusBarOrientation];
[self willRotateToInterfaceOrientation:orientation duration:1.0];
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> content)
{
[self willAnimateRotationToInterfaceOrientation:orientation duration:1.0];
}
completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
[self didRotateFromInterfaceOrientation:oldOrientation];
}];
CGFloat height = self.view.frame.size.height;
CGFloat width = self.view.frame.size.width;
[self.view setFrame:CGRectMake(0, 0, height, width)];
}
interfaceFromTransform is a method I created to determine which interface the transform is rotating to:
- (UIInterfaceOrientation) interfaceFromTransform: (CGAffineTransform)transform
{
UIInterfaceOrientation old = [[UIApplication sharedApplication] statusBarOrientation];
int rotation = 0;
if (transform.b == -1 && transform.c == 1)
rotation = 90;
if (transform.b == 1 && transform.c == -1)
rotation = -90;
if (transform.a == -1 && transform.d == -1)
rotation = 180;
if (old == UIInterfaceOrientationLandscapeRight)
{
if (rotation == 90)
return UIInterfaceOrientationPortrait;
if (rotation == -90)
return UIInterfaceOrientationPortraitUpsideDown;
if (rotation == 180)
return UIInterfaceOrientationLandscapeLeft;
}
if (old == UIInterfaceOrientationLandscapeLeft)
{
if (rotation == 90)
return UIInterfaceOrientationPortraitUpsideDown;
if (rotation == -90)
return UIInterfaceOrientationPortrait;
if (rotation == 180)
return UIInterfaceOrientationLandscapeRight;
}
if (old == UIInterfaceOrientationPortraitUpsideDown)
{
if (rotation == 90)
return UIInterfaceOrientationLandscapeRight;
if (rotation == -90)
return UIInterfaceOrientationLandscapeLeft;
if (rotation == 180)
return UIInterfaceOrientationPortrait;
}
if (old == UIInterfaceOrientationPortrait)
{
if (rotation == 90)
return UIInterfaceOrientationLandscapeLeft;
if (rotation == -90)
return UIInterfaceOrientationLandscapeRight;
if (rotation == 180)
return UIInterfaceOrientationPortraitUpsideDown;
}
return old;
}
Yet despite all this, that white space still refuses to go away. Is there something obvious that I'm missing that will keep it from appearing when I rotate the screen?
As it turns out, I had disabled "Autoresize Subviews" in my main window nib file. Enabling this immediately fixed the problem.

change screen rotation of app extension in objective c

How do I lock from rotating screen, I want my app extension to be only in portrait mode.
When I'm using my extension inside Photos my I can rotate the screen to landscape.
thanks
You have to choice:
1- Set landscape and portrait as supported interface orientation in the project, and then for each ViewController, you will override the supported interface orientation;
2- Set only portrait mode in the project, and in the ViewController you need it, you can make a 90 degree rotation
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
EDIT: so you can do this. Set your controller as observer for the keyPath UIDeviceOrientationDidChangeNotification. Then:
-(void)changedOrientation: (NSNotification *)note
{
if (note)
{
UIDevice *dev = (UIDevice *)note.object;
if ([dev orientation] == UIInterfaceOrientationLandscapeRight)
{
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
}
else if ([dev orientation] == UIInterfaceOrientationPortrait)
{
self.view.transform = CGAffineTransformIdentity;
}
else if ([dev orientation] == UIInterfaceOrientationPortraitUpsideDown)
{
self.view.transform = CGAffineTransformIdentity;
}
else if ([dev orientation] == UIInterfaceOrientationLandscapeLeft)
{
self.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
}
}
}

alignment for different ios device

I am new to iOS programming. I made an application for iPhone retina 4-inch. Now I want to customise my code in order for it to run on an iPhone retina-3.5 inch and iPhone 5.
I tried to create an if query for self.view.frame.size.width and self.view.frame.size.height, but I get a "Not assignable" error.
Can anyone tell me how to specify conditions for setting up the workspace nicely in all devices? If possible please give me the exact screen-size of all that devices.
You can use this:
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
References:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIScreen_Class/Reference/UIScreen.html
You look at the device's screen size (in points) and from that surmise if it's an iPad or iPhone etc., and then use hard-coded values for the screen sizes.
Here's some code to get the screen size:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
Be aware that width and height might be swapped, depending on device orientation.
to compare screen size include this macro and use it any where necessary
#define isPhone5 ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568)
if you want to compare screen sizes only then reduce the above macro to
#define isiPhone5 ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE
For example in your .m file
#import "ViewController.h"
#define isiPhone5 ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE //hear u put the macro and use it anywhere in this file
#interface ViewController ()<FootballPlayerDelegate>//confirms to this delegate
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
if(isiPhone5)
{
NSLog(#"I am 4inch screen");
}
else
{
NSLog(#"I am 3.5inch screen");
}
// Do any additional setup after loading the view, typically from a nib.
}
Try this In your .M file:
-(void)viewWillAppear:(BOOL)animated
{
[self willAnimateRotationToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:1.0];
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft)
{
[self setFrameForLeftAndRightOrientation];
}
else if(orientation == UIInterfaceOrientationPortrait )
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
- (void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
[self setFrameForLeftAndRightOrientation];
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
-(void)setFrameForLeftAndRightOrientation
{
if(IS_IPAD)
{
//Write here code for iPad (Landscape mode)
}
else if(IS_IPHONE)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
//Write code here for iPhone 5 (Landscape mode)
}
else
{
//Write code here for iPhone(3.5 inch) (Landscape mode)
}
}
}
-(void)setFrameForPortraitAndUpsideDownOrientation
{
if(IS_IPAD)
{
}
else if(IS_IPHONE)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
}
else
{
}
}
}
#define IS_IPHONE_SIMULATOR ([[[UIDevice currentDevice]model] isEqualToString : #"iPhone Simulator"])
#define IS_IPHONE ([[[UIDevice currentDevice]model] isEqualToString : #"iPhone"])
#define IS_IPOD_TOUCH ([[[UIDevice currentDevice]model] isEqualToString : #"iPod Touch"])
#define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
#define ITS_IPHONE5 ( IS_HEIGHT_GTE_568 )
if (ITS_IPHONE5)
{
}
else
{
}
Use This Code in project-Prefix.pch file in supporting file folder.
#define IS_IPHONE_SIMULATOR ([[[UIDevice currentDevice]model]
isEqualToString : #"iPhone Simulator"])
#define IS_IPHONE ([[[UIDevice currentDevice]model] isEqualToString : #"iPhone"])
#define IS_IPOD_TOUCH ([[[UIDevice currentDevice]model] isEqualToString : #"iPod Touch"])
#define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
#define ITS_IPHONE5 ( IS_HEIGHT_GTE_568 )
Copy and Past This Code After That in your .m File past the first code i give you...
Dont need to write size on viewdidload write only in these cots
if (IS_IPAD)
{
}
else if(IS_IPHONE)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
}
else
{
}
}

set the frame size when orientation is changed in ios

I want to change the orientation of a view in ios 6. When the orientation is changed, I want to set the frame size of the views.
My Sample Code for orientation change:
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSInteger)supportedInterfaceOrientations
{
NSInteger mask = 0;
intOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(intOrientation == UIInterfaceOrientationPortrait || intOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
mask |= UIInterfaceOrientationMaskPortrait;
} else
if(intOrientation == UIInterfaceOrientationLandscapeLeft)
{
mask |= UIInterfaceOrientationMaskLandscapeLeft;
}
}
NSLog (#"mask %d",mask);
return mask;
}
// Here now mask value is 2 because my default mode is portrait.
Now when I change my orientation to landscape, I want to set the frame size. Please guide me on how to do that.
ok..Can I set the frame size for the view like dis by taking the version and comparing it in viewdidLoad:
orientation = [[UIDevice currentDevice] orientation];
NSString *reqSysVer = #"6.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
NSLog (#"ios version %#",currSysVer);
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
{
if (orientation == UIInterfaceOrientationMaskPortrait) {
self.view.frame = CGRectMake(0, 0, 1000, 800);
self.tableView1.frame = CGRectMake(1, 0, 764,510);
}
else if (orientation == UIInterfaceOrientationMaskLandscapeLeft)
{
self.view.frame = CGRectMake(0, 0, 1300, 370);
self.tableView1.frame = CGRectMake(0, 0, 1300, 370);
self.view.backgroundColor = [UIColor blackColor];
}
}
you can get a notification when the interface orientation changes:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
and add a function like:
- (void)didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientation...)
{
// add some code
}
}
The best solution would be to use Auto Layout or Constraints, I think.
But you can also check for the device orientation using
[UIDevice currentDevice].orientation
or check the size (e.g. for iPhone 5 support) using
[UIScreen mainScreen].bounds.size.height
// Update:
Consider that on iOS 8 width and height were switched in landscape. So witdh and height are always depending on the orientation now.

How to rotate landscape only with Cocos2d

This is my last step of the app, everything else is done.
I'm using cocos2d, and no matter what I do, I cannot get the app to rotate. It HAS to only rotate landscape with the way my app is set up.
Here's my GameConfig.h
#ifndef __GAME_CONFIG_H
#define __GAME_CONFIG_H
//
// Supported Autorotations:
// None,
// UIViewController,
// CCDirector
//
#define kGameAutorotationNone 0
#define kGameAutorotationCCDirector 1
#define kGameAutorotationUIViewController 2
//
// Define here the type of autorotation that you want for your game
//
#define GAME_AUTOROTATION kGameAutorotationUIViewController
#endif // __GAME_CONFIG_H
And my AppDelegate's applicationDidFinishLaunching method,
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
//[director setDeviceOrientation:kCCDeviceOrientationPortrait];
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#else
// [director setDeviceOrientation:kCCDeviceOrientationPortrait];
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#endif
And RootController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return ( UIInterfaceOrientationIsLandscape( interfaceOrientation ) );
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//
// Assuming that the main window has the size of the screen
// BUG: This won't work if the EAGLView is not fullscreen
///
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect rect = CGRectZero;
if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
rect = screenRect;
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );
CCDirector *director = [CCDirector sharedDirector];
EAGLView *glView = [director openGLView];
float contentScaleFactor = [director contentScaleFactor];
if( contentScaleFactor != 1 ) {
rect.size.width *= contentScaleFactor;
rect.size.height *= contentScaleFactor;
}
glView.frame = rect;
}
In the Info.plist I have both landscape orientations in the supported list.
What am I doing wrong?
Thanks
Try to add this line
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
to your applicationDidFinishLaunching: method

Resources