change screen rotation of app extension in objective c - ios

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);
}
}
}

Related

Rotate the view only in ios

My application is only supported on Portrait mode. I have requirement that need to rotate the particular UIView only according to the device orientation.
Can you please help me?
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
// set your view frame work for portrait mode
}
else {
// set new frame of view for landscape mode.
}
Hope you get the idea.
thanks
Add below give code in your viewdidload or init
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];
In deviceOrientationDidChange function you will get the device orientation,according to this you can update your view
- (void)deviceOrientationDidChange:(NSNotification *)notification {
//Obtain current device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
//Do my thing
}
After the long try I found the answer using CGAffineTransform ..
- (void)deviceOrientationDidChange:(NSNotification *)notification {
//Obtain current device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
CGFloat angle;
switch (orientation) {
case UIDeviceOrientationPortraitUpsideDown:
angle = M_PI;
break;
case UIDeviceOrientationLandscapeRight:
angle = - M_PI_2;
break;
case UIDeviceOrientationLandscapeLeft:
angle = M_PI_2;
break;
case UIDeviceOrientationPortrait:
default:
angle = 0.f;
break;
}
animationView.transform= CGAffineTransformMakeRotation(angle);
animationView.frame = self.view.frame;
}

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.

How to get the devices UIInterfaceOrientation

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),
};

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.

My modal view does not rotate

I encounter an issue with a modal view display. My first view rotate very well. The controller of the modal view implements the "ShouldAutorotationToInterfaceRotation" method but when I rotate the device or simulator the modal view doesn't rotate.
I've added some comments to check if the first view detects the rotation events instead of the modal view but no, it doesn't get the event.
Do you have a clue?
You will need to subscribe to NSNotificationCenter to receive rotation events for your modal view. Then, if you want to keep the modal view orientated correctly relative to the bottom view, one solution would be to transform the view. Here's one implementation:
Put this in the viewDidLoad and save the original orientation as an angle:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
int initialOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (initialOrientation == UIDeviceOrientationPortrait)
initialAngle = 0.0;
else if (initialOrientation == UIDeviceOrientationLandscapeRight)
initialAngle = 90.0;
else if (initialOrientation == UIDeviceOrientationPortraitUpsideDown)
initialAngle = 180.0;
else if (initialOrientation == UIDeviceOrientationLandscapeLeft)
initialAngle = 270.0;
currentAngle = initialAngle;
Then define the function orientationChanged:(NSNotification *)notification with code to rotate correctly relative to the initial orientation:
int newOrientation = [[notification object] orientation];
if (newOrientation == UIDeviceOrientationPortrait)
desiredAngle = 0.0;
else if (newOrientation == UIDeviceOrientationLandscapeRight)
desiredAngle = 90.0;
else if (newOrientation == UIDeviceOrientationPortraitUpsideDown)
desiredAngle = 180.0;
else if (newOrientation == UIDeviceOrientationLandscapeLeft)
desiredAngle = 270.0;
if(desiredAngle != currentAngle)
{
CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI * (-desiredAngle+initialAngle) / 180.0);
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, 0);
[[self releaseView] setTransform:CGAffineTransformConcat(translate, rotate)];
}
currentAngle = desiredAngle;

Resources