change device orientation compatible for ios6 and ios5 - ios5

I NEED: rotate device programmatically from portrait to landscape compatible for iOS6 and iOS5.
THE PROBLEM: I cannot use [UIDevice setOrientation] function on ios6.
WHAT I HAVE: the code for iOS6:
- (BOOL) shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
the code for iOS5:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
So, I need write the code that rotate on iOS5:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// [[UIDevice currentDevice] setOrientation] - I cannot use it
// ???
}

I achieved this by set Status bar orientation, try execute in code:
[[sharedApplication] setStatusBarHidden:YES];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
and when you need portrait mode, set status bar to setStatusBarHidden:NOand again set statusbar orientation to portrait UIInterfaceOrientationPortrait.

Related

Force landscape for one view controller ios

I've looked at several answers to questions similar but none of the answer worked. I have an app where I need everything portait except for one photo viewer I have. In the supported orientations section of the targets menu I only have portrait. How do I force my one view to be landscape. It is being pushed onto the stack from a nav controller but I'm using storyboards to control all that.
Since the answer seems to be hidden in the comments of the question and since ArunMak's answer is quite confusing, I'll just offer what I found out:
All I had to do was to add this function to my custom UIViewController subclass for the view:
- (NSUInteger)supportedInterfaceOrientations {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
// iPad: Allow all orientations
return UIInterfaceOrientationMaskAll;
} else {
// iPhone: Allow only landscape
return UIInterfaceOrientationMaskLandscape;
}
}
Note that the project needs to allow all orientations (that is: Portrait, Landscape Left, Landscape Right - but NEVER Upside Down on an iPhone!).
If you want to limit some or most views to Portrait, you need to implement the above method in every of those view controllers (or use a common super class for it and subclass all others from it) — if you limit the Device Orientation in the Info.plist to just Portrait, the app will never even think of going into landscape.
Yes this is possible, you can use this code:
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskLandscape;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
return orientation==UIInterfaceOrientationMaskLandscape;
}
OR
Try this method in your app delegate
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (sglobalorientation isEqualToString:#"AllOrientation"]) {
return UIInterfaceOrientationMaskLandscape;
} else {
return UIInterfaceOrientationMaskAll;
}
}
you need to change the variable value sglobalorientation to that string value AllOrientation before you move to that Landscape view controller
and in your Landscape view controller, use this code in your view will appear
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
DigitalSignatureViewController *digisign = [[DigitalSignatureViewController alloc]init];
[self presentModalViewController:digisign animated:NO];
[self dismissModalViewControllerAnimated:NO];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
and again when you move to next view controller change the sglobalorientation string value and follow the same step in your next view controller.
Lets try this code:
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
self.navigationController.view.center = CGPointMake(([UIScreen mainScreen].bounds.size.width/2), [UIScreen mainScreen].bounds.size.height/2);
CGFloat angle = 90 * M_PI / 180;
self.navigationController.view.transform = CGAffineTransformMakeRotation(angle);
self.navigationController.view.bounds = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.height , [UIScreen mainScreen].bounds.size.width);

Orientation should change only for the selected viewcontroller

In my app, Four viewcontrollers navigates next by next in potrait mode, but i added coverflow in the final viewcontroller, and when the simulator is rotated it should go landscape.I selected all orientations in plist execpt the Upsidedown orientation, So that coverflow works fine in Landscape, but all the other viewcontrollers also goes to landscape when rotatad, but i need these viewcontrollers to be in potrait even the simulator is rotated.I tried many codes like,
shouldAutorotate and supportedInterfaceOrientations.
But i doesn't get a clear solution towards what iam expecting, if any ideas , will be thankfull.
Add an observer to the viewDidLoad method of the view you want to rotate like this :
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
and then set the views according the the landscape view inside the orientationChanged method like this :
- (void) orientationChanged:(NSNotification *)note{
UIDevice * device = [UIDevice currentDevice];
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
break;
case UIDeviceOrientationPortraitUpsideDown:
break;
case UIDeviceOrientationLandscapeLeft:
break;
case UIDeviceOrientationLandscapeRight:
break;
default:
break;
};
}
Add new Objective-C class (subclass of UINavigationController) and add the following code to the .m files
-(NSUInteger)supportedInterfaceOrientations
{
NSLog(#"supportedInterfaceOrientations = %d ", [self.topViewController supportedInterfaceOrientations]);
return [self.topViewController supportedInterfaceOrientations];
}
-(BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// You do not need this method if you are not supporting earlier iOS Versions
return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
After you added the new classes go to your ViewController classes and make the following changes
- (BOOL)shouldAutorotate // iOS 6 autorotation fix
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations // iOS 6 autorotation fix
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
In the shouldAutorotate , shouldAutorotateToInterfaceOrientation: return YES if you want the ViewController to be supporting Multiple orientation else return NO , also in houldAutorotateToInterfaceOrientation: method pass the Orintation you want for that specific ViewController , Repeat the same for all the view controllers .
Reason of doing this:-
1:Although you can change the preferredInterfaceOrientationForPresentation: of any viewController to a specific orientation but since you are using the UINavigationController you also need to override the supportedInterfaceOrientations for your UINavigationController
2:In order the override the supportedInterfaceOrientations for UINavigationController we have subclassed UINavigationController and modified the method related to the UINavigation Orientation.
Hope it will help you !
In iOS 6 and later, the view controllers responsible for rotation are the container Viewcontrollers such as UINavigationController & UITabBarController . What are you using as the rootviewcontroller in your project??
I have navigation controller category written here in which you can handle the supported interface orientations for the individual view controllers.
viewDidLoad or viewWillApper method add following code
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
set the orientation
#if (__IPHONE_6_0 >= __IPHONE_OS_VERSION_MAX_ALLOWED)
- (NSInteger)supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskPortrait);
}
#endif
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation==UIInterfaceOrientationPortrait);
}

Iphone landscape and portrait issue

I m working on one application in which i have splash screen and home screen. My Application is landscape and portrait mode application. When my application starts and i am on Landscape mode my home screen shows portrait mode for some mili seconds and after that it comes into the landscape mode. I want it should directly show the view in landscape mode.
Thanks
I usually enable notifications:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
On my controllers i put:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self configureOrientation:toInterfaceOrientation];
}
- (void)configureOrientation:(UIInterfaceOrientation)orientation
{
if(UIDeviceOrientationIsValidInterfaceOrientation(orientation)){
if(UIInterfaceOrientationIsLandscape(orientation)){
[self configureLandscapelView];
}else if(UIInterfaceOrientationIsPortrait(orientation)){
[self configurePortraitlView];
}
}
}
You can also get the orientation from the UIViewController itself:
UIInterfaceOrientation orientation = [self interfaceOrientation];
Or from the status bar:
[UIApplication sharedApplication] statusBarOrientation]

Reverting to portrait after Done pressed on movie player?

I've got a MPMoviePlayer working. It is designed to show a postage-stamp size movie as a subview in the view. When the phone is rotated into landscape, it goes into full screen mode. And when the phone is in portrait it goes into postage-stamp portrait mode.
The only problem is when I press Done when in landscape mode, it stays in landscape, with the postage stamp sized movie, rather than kick back into portrait..
Here's some of my code:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
[moviePlayerController setFullscreen:YES animated:YES];
} else
{
[moviePlayerController setFullscreen:NO animated:YES];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft;
}
How would I get it to kick into portrait mode after pressing Done?
#cannyboy...you just need to use below method in your APPDelegate.m if your application only works with portrait mode
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if ([[window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
//NSLog(#"in if part");
return UIInterfaceOrientationMaskAllButUpsideDown;
}
else
{
//NSLog(#"in else part");
return UIInterfaceOrientationMaskPortrait;
}}
I was the same problem and now I am telling you how I solved it. I don't know the below method is right or wrong but it work fine.
Instead of opening MPMoviePlayer in view open it in a new viewController. I mean create a new UIViewController to show the movie and push it some how so that user will not understood that they are redirecting into a new screen.
Disable the parent screen for landscape mode and allow MovieViewController to landscape.
When user press the done button or close button simply pop the viewController and as the previous screen don't support landscape mode so the screen will automatically shows in portrait mode.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(_moviePlayerWillExitFullscreen:)
name:MPMoviePlayerWillExitFullscreenNotification object:nil];
- (void)_moviePlayerWillExitFullscreen:(NSNotification *)notification
{
CGFloat ios = [[[UIDevice currentDevice] systemVersion] floatValue];
CGFloat min = 5.0;
if (ios >= min)
{
if (self.interfaceOrientation != UIInterfaceOrientationPortrait)
{
if([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait])
{
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
[self willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];
[UIViewController attemptRotationToDeviceOrientation];
}
}
}
}
Note that this only works in ios 5.0 and later, and you will get a warning that setOrientation is not supported, but it works pretty well
One way you could try is to force your device to think it is in portrait mode. To do this, try using:
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
If you do not want your device to show landscape while not playing a movie, you will also have to add some logic to your code that you have shown above to only change to landscape if the movie is playing.

iPhone device orientation problem

My application is Navigation based.
All views are displaying Portrait mode except Report mode.Report mode display landscape mode when device is rotate landscape.
If device is rotate landscape mode Report view is displaying landscape mode.if report is landscape mode once again rotate in device Portrait mode it will display normal view of current view.
Flow of current action my view display.
From current view is Portrait mode and i am rotating device in landscape mode so getting Landscape mode of Report mode. after two rotate only i am getting current view in Portrait mode. I need to reduce tow rotation . please guide me . How to check condition for after landscape mode of Report if once again rotate i need to display current view in Portrait mode.
Here at ReportViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
#interface CurrentViewController
BOOL isShowingLandscapeView;
Report *landscapeViewController;
#implementation CurrentViewController
- (void)viewDidLoad
{
[super viewDidLoad];
Report *viewController = [[Report alloc]initWithNibName:#"Report" bundle:nil];
self.landscapeViewController = viewController;
[viewController release];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
[self performSelector:#selector(updateLandscapeView) withObject:nil afterDelay:0];
}
- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
[self presentModalViewController:self.landscapeViewController animated:YES];
isShowingLandscapeView = YES;
}
else if(deviceOrientation == UIInterfaceOrientationPortrait && isShowingLandscapeView)
{
[self dismissModalViewControllerAnimated:YES];
isShowingLandscapeView = NO;
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait ); // support only portrait
}
I'm not sure I understand your question, but the code looks like it might have the logic reversed.
In the ReportViewController,
- (BOOL)shouldAutorotateToInterfaceOrientation
is called before the rotation, so it should
return YES for Portrait (YES, allow the view to rotate from Landscape TO portrait)
and return NO for Landscape (NO, do not allow the view to rotate to Landscape from Landscape).
And similarly in CurrentVC - try
return (interfaceOriention==UIInterfaceOrientationLandscape);
Hope that helps.
-Mike