I am trying to disable screen rotation in cocos2d-3.x, but I cannot figure out how. I have the following code in CCAppDelegate:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
setupCocos2dWithOptions:
[self setupCocos2dWithOptions:#{
CCSetupShowDebugStats: #(YES),
// Run in portrait mode.
CCSetupScreenOrientation: CCScreenOrientationPortrait,
}];
return YES;
}
The screen is in portrait mode, but it also rotates to portrait UpsideDown. I read that Cocos2d uses a UIViewController so should I use an apple method for this?
Cocos2D currently only allows to choose between "Landscape" or "Portrait" but doesn't allow to specify the orientation more specific. This likely will be fixed in Cocos2D but for now you can modify the Cocos2D source code as a workaround. I have opened a GitHub Issue for this problem.
Implementing supportedInterfaceOrientations in CCAppDelegate does not fix the issue because the method is implemented by the CCNavigationController and that overrides your settings.
Open "CCAppDelegate.m" and go to line 77. You should see this method:
-(NSUInteger)supportedInterfaceOrientations
{
if ([_screenOrientation isEqual:CCScreenOrientationAll])
{
return UIInterfaceOrientationMaskAll;
}
else if ([_screenOrientation isEqual:CCScreenOrientationPortrait])
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
else
{
return UIInterfaceOrientationMaskLandscape;
}
}
You can change the entire method to only support one orientation, e.g.:
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationPortrait;
}
When you change that method you can return one of the 4 orientations you want to lock your game into:
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskPortraitUpsideDown
That's the workaround for now.
Related
For the app, the orientation is in portrait most of the time, but we have one view that needs to be set to landscape. I have this code to set it to landscape:
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
I also have device orientation set to portrait and landscape left in the general tab for the app.
The issue is that the app will sometimes work properly but other times it will get stuck in a mix between portrait and landscape.
I have provided an image of what it looks like getting Stuck in transition. If I then reload the view it will work properly, it is only on the first time opening the view that it has a chance of failing.
I think you should set this in AppDelegate.m
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
if self.rotatble {
return UIInterfaceOrientationMaskLandscape
} else {
return UIInterfaceOrientationMaskPortrait
}
I have an iOs project that needs to support all orientations on iPad and on iPhone I need to support only portrait on all view controllers EXCEPT one that needs to support all orientations and autorotate.
I tried:
#pragma mark - Should Rotate device
- (NSUInteger)supportedInterfaceOrientations
{
if ([[DeviceInfo sharedInstance] is_iPhone]) {
return UIInterfaceOrientationMaskPortrait;
} else {
return UIInterfaceOrientationMaskAll;
}
}
But that method doesn't get called...
How should I do this?
Why don't you select the wanted ones here:
Select your app in project navigator -> general -> select your target -> scroll down to deployment info.
If my understanding is right then I have given answer for this query so u could follow this link.
Do let me know if any doubt.
Okay so got solution for your iOS 7 as below method write down in appDelegate class.
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if ([[[[[[UIApplication sharedApplication] keyWindow] rootViewController] childViewControllers] lastObject] isKindOfClass:[ViewController class]]) //Here you could provide your view controller and provide orientation which u want for that particular view controller.
{
return UIInterfaceOrientationMaskPortrait;
}
else
{
return UIInterfaceOrientationMaskLandscape;
}
}
This will help you out for iOS 7.
The problem was solved by implementing the methods on the navigation controller that owns all the views
I am making game which supports only landscape orientation but I am also using a library to share my game video but that share screen require portrait orientation , If I don't enable portrait orientation my game got crash but If I enable portrait orientation to avoid this crash then my whole game become useless by becoming portrait as it is only for landscape.
This is my game Landscape View as shown by figures below,
This is the Library Portrait View to Share video
My Game View after sharing video from library
Please help me How can I enable portrait orientation for this library to avoid crash
and my rest of the app always remain in landscape and it never goes to portrait orientation.
Thanks
Add the following method to your application's AppDelegate.m file:
// IOS 6
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskAll;
}
In both cases you must also make sure that you have added the landscape only orientation handling code to your game's main UIViewController.
// IOS 5
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
// IOS 6
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
Try to implement orientation methods to your view controller.
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html
I hope this help you.
In the view controller you want to support orientation override supportedInterfaceOrientations method.
I did like this...
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}
i am working on a app where I am required to show "MPMoviePlayerController" in landscape mode and portrait mode. But My whole app is required to support Portrait mode only. That is no landscape mode for any view other than for the "MPMoviePlayerController".
I tried few things given over stack overflow. Nothing worked in my case. Feels Stuck in the middle. But I have seen some of the app supporting suck kind of requirements.
I have to implement it for both iOS 6, 7
In my app am using "XCDYouTubeVideoPlayerViewController" for playing videos(playing the youtube videos)
Please Help
I had the same issue, and the following solved the problem:
First you need to allow the Landscape mode either, by checking the checkboxes at Target / General / Deployment Info / Device orientation, and then you have to disable Landscape orientation by code at every ViewController you use in your app.
#pragma mark - Set Supported Device Orientation
//For iOS6
- (BOOL)shouldAutorotate {
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
//For iOS4 and iOS5
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
But don't disable landscape orientation for the XCYoutubeVideoPlayerViewController, so at fullscreen it can rotate to landscape.
I have another solution for this, It will work for all MPMoviePlayerController, below is my code
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)windowx
{
if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]] ||
[[self.window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(#"MPInlineVideoFullscreenViewController")])
{
if ([self.window.rootViewController presentedViewController].isBeingDismissed)
{
return UIInterfaceOrientationMaskPortrait;
}
else
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
}
else
{
return UIInterfaceOrientationMaskPortrait;
}
}
What we basically do here is we enable Landscape orientation for all MPMoviePlayerController classes which are actually MPInlineVideoFullscreenViewController when you present it for fullscreen.
Hope this helps
My code is working fine in iOS 5.1 but not in iOS 6. I am getting orientation problems.
I am using this code for controlling the orientations
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
I want to show one view controller in Landscape mode and other viewController in Portrait mode
Any one can help me with this?
Thanks
Try look at this
iOS 6 Tips and Tricks Upgrading Your Apps
OR
try to add this codes in Main viewcontroller:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight;
}
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)w {
return (NSUInteger)[application supportedInterfaceOrientationsForWindow:w] | (1<<UIInterfaceOrientationPortrait);
}
And main viewcontroller must be added how:
[window setRootViewController: mainController];
instead
[window addSubview: mainController.view];