I have used two classes one is rootviewcontroller.mm for landscape mode and another one is rootviewcontrollerV.m (for horizontal) which is inherited from rootviewcontroller.mm.
I have received the event from cocos to ios in appcontroller.mm to change the orientation - the method -
+(void)changeOrientationH:(BOOL)val{
//[AppController scanFingerPrint];
RootViewController* nextViewController = nil;
CCEAGLView* glView = nil;
if(val == NO){
glView = (CCEAGLView *)appController->_viewController.view;
appController->_viewController.view = nil;
appController->_viewControllerV.view = glView;
nextViewController = appController->_viewControllerV;
}
else{
glView = (CCEAGLView *)appController->_viewControllerV.view;
appController->_viewControllerV.view = nil;
appController->_viewController.view = glView;
nextViewController = appController->_viewController;
}
appController->_navController = nil;
appController->_navController = [[UINavigationController alloc]initWithRootViewController:nextViewController];
// Set RootViewController to window
if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
{
// warning: addSubView doesn't work on iOS6
[appController->window addSubview: appController->_navController.view];
}
else
{
[appController->window setRootViewController:appController->_navController];
// use this method on ios6
}
CGRect bounds = [UIScreen mainScreen].bounds;
// NSLog(@"Main Screen %@",bounds);
float scale = [[UIScreen mainScreen] scale];
float width = bounds.size.width * scale;
float height = bounds.size.height * scale;
app->updateViewSize(width, height);
[AppController onOrientationDoneCallback];
//EventDispatcher::dispatchResizeEvent(width, height);
//cocos2d::EventDispatcher::dispatchResizeEvent(width, height);
}
I’m also adding rootviewcontroller file code -
#import “RootViewController.h”
#import “AppController.h”
#import “cocos2d.h”
#include “platform/CCApplication.h”
#include “platform/ios/CCEAGLView-ios.h”
#include “scripting/js-bindings/event/EventDispatcher.h”
#include “cocos/scripting/js-bindings/jswrapper/SeApi.h”
@interface RootViewController ()
@end
@implementation RootViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-
(void)viewDidLoad {
[super viewDidLoad];
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationMaskLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@“orientation”];
CCEAGLView *view = ((__bridge CCEAGLView *)cocos2d::Application::getInstance()->getView());
self.view = view;
CGRect bounds = [[UIScreen mainScreen] bounds];
UIImageView *backView = [[UIImageView alloc] initWithFrame: bounds];
UIImage *img = [self getLaunchImage];
backView.contentMode = UIViewContentModeScaleAspectFit;
[backView setImage:img];
[self.view addSubview:backView];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:true];
}
// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead
#ifdef __IPHONE_6_0
- (NSUInteger) supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
#endif
//- (BOOL) shouldAutorotate {
// return YES;
//}
//
//- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
// [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
//
// CGRect bounds = [UIScreen mainScreen].bounds;
// float scale = [[UIScreen mainScreen] scale];
// float width = bounds.size.width * scale;
// float height = bounds.size.height * scale;
//
// //EventDispatcher::dispatchResizeEvent(width, height);
// cocos2d::EventDispatcher::dispatchResizeEvent(width, height);
//}
//fix not hide status on ios7
- (BOOL)prefersStatusBarHidden {
return YES;
}
// Controls the application’s preferred home indicator auto-hiding when this view controller is shown.
-
(BOOL)prefersHomeIndicatorAutoHidden {
return YES;
}
-
(void)didReceiveMemoryWarning {
// Releases the view if it doesn’t have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren’t in use.
}
+(BOOL)callNativeUIWithTitle:(NSString *) title andContent:(NSString *)content{
RootViewController *rootVC = [[RootViewController alloc] init];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:content delegate:rootVC cancelButtonTitle:@“Cancel” otherButtonTitles:@“OK”, nil];
[alertView show];
[alertView release];
return true;
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@“alert clicked”);
// [AppController runJavaScript];
}
-(UIImage *) getLaunchImage
{
NSArray *allPngImageNames = [[NSBundle mainBundle] pathsForResourcesOfType:@“png”
inDirectory:nil];
for (NSString *imgName in allPngImageNames){
// Find launch images
if ([imgName containsString:@"LaunchImage"]){
UIImage *img = [UIImage imageNamed:imgName];
// Has image same scale and dimensions as our current device's screen?
if (img.scale == [UIScreen mainScreen].scale && CGSizeEqualToSize(img.size, [UIScreen mainScreen].bounds.size)) {
NSLog(@"Found launch image for current device %@", img.description);
return img;
}
}
}
return nil;
}
@end
and rootviewcontrollerV.m file code ----
//
// RootViewControllerV.m
#import “RootViewControllerV.h”
#import “AppController.h”
@interface RootViewControllerV ()
@end
@implementation RootViewControllerV
// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead
#ifdef __IPHONE_6_0
- (NSUInteger) supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
#endif
[super viewDidLoad];
// self.view.backgroundColor = UIColor.blueColor;
// Do any additional setup after loading the view.
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
//- (void)loadView {
// [super loadView];
//}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
this code is perfectly working with 2.4.0 with Xcode11 but when I update from 2.4.0 to 2.4.7, it’s not working