I want to rotate my app upside down portrait using the following code in the ViewController:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{ //return NO;
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationMaskPortraitUpsideDown);
}
- (UIInterfaceOrientationMask) supportedInterfaceOrientations
{
return [super supportedInterfaceOrientations] | UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskPortrait;
}
In the AppDelegate:
- (UIInterfaceOrientationMask) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskPortrait;
}
When the user presses a button
NSNumber * value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
The app does somehow not rotate and I have no idea why.
Somebody can give a hint?
Thank you
I added the orientations in the info.plist and set the checkboxes in the general settings in deployment info section.
Do I understand you correctly, that you want to programatically want to rotate the user interface, independend from the physical orientation of the device?
This is not how it works: The delegate methods (shouldAutorotate... etc.) are called by iOS when a (physical) rotation of the device is detected, e.g. the user turns it upside down. In the delegate methods you then get the chance to adopt the views to the new orientation, if you need to do so.
In the simulator, you can simulate device rotation by Alt+Left / Alt+Right keys.
You cannot rotate the orientation programatically it depends on sensors of the device. However you can rotate the layer of the view.
self.view.layer.transform = CGAffineTransformMakeRotation(M_PI_2);
Related
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.
I am develop a IOS Application, I use the following code for lock the orientation of App.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
If the orientation is UIInterfaceOrientationPortrait When start the App. It will lock the orientation to UIInterfaceOrientationPortrait.
But the view still show Landscape , when start the App in Landscape.
How to set the Orientation of App to UIDeviceOrientationIsPortrait for App start whether the phone is Landscape or 'Portrait`?
If you need to lock the whole app to Portrait then go to TARGETS -> General Tab -> Deployment Info
This will lock the whole app to Portrait Mode
If you only need one orientation for the entire app then go to targets, General, and deployment info. Then check portrait.
I use the following code for setting Orientation to Portrait.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated] ;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
//set the orientation is Portrait when the App start.
if ([[UIDevice currentDevice] respondsToSelector:#selector(setOrientation:)]) {
objc_msgSend([UIDevice currentDevice], #selector(setOrientation:), UIInterfaceOrientationPortrait );
}
}
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 have an app that uses the proximity sensor but the proximity sensor does not work in landscape mode. I have heard that if you keep the status bar in portrait mode it the sensor will work
I have tried this but it did not work.
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
The status bar still switched into landscape mode.
What do I do?
If you would like to keep your app in portrait mode regardless of DeviceOrientation, I would suggest you add the following code to your viewController.
- (BOOL) shouldAutorotate{
return NO;
}
- (NSUInteger) supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;
}
This will ensure that your app will start in portrait mode, and stay in portrait mode, even if the device is turned.
EDIT: In order to keep the app in landscape while having just one view on portrait mode, add the above code to the one viewController you would like to restrict to portrait mode.
Add the following function to your appDelegate:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
/* This is in order to allow views that do not support landscape mode to be able to load in
*/
return UIInterfaceOrientationMaskAll;
}
Add the following code to your other view controllers:
- (BOOL) shouldAutorotate{
return YES;
}
- (NSUInteger) supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeRight; //you can choose either landscape orientation here
}
I suggest you use this hierarchy:
RootViewController - it will contain the reference (preferably
strong) to your proximity sensor, which I recommend you run on a
background thread. This will support only portrait mode, so your
proximity sensor will work ok (I hope).
displayViewController - it will contain all the UI elements you have, and support only landscape mode
You could manage communication between both of them by using delegates
Also, after the viewDidApper of RootViewController, please use the presentViewController (iOS 6.0 and above) or presentModalViewController (iOS 5.x) selector to add the displayViewController's view on screen. Using [self.view addSubview: displayViewController.view]; will not work (I speak from personal experience).
My app is a universal application that supports portrait and portrait upside down orientations.
My app uses a UITabBarController that has UINavigationControllers for 3 UITabBarItems.
To support of of these, i have done the following things:
1) added "supported Interface Orientations" key accordingly for both devices
2) added the following code in app delegate and all other view controllers
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}
Everything is going as expected in iPad, but on iPhone, only some of my views (not all) do not respond to change in Orientation..!! Why is this happening??
You should check that all of your view controllers have the rotation defined, and then you should add this to them:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
If you're using a navigation controller, you should create a subclass .i.e. "MyNavController" with the instructions to rotate in its implementation:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}
and then use it:
MyNavController *navo = [[MyNavController alloc] initWithRootViewController:myRootController];
As far as I know upside down in portrait mode for the IPhone is not allowed, cause when a user receives a phonecall, the app is on hold and he has the phone upside down. Thats why apple does not want that.