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 );
}
}
Related
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);
The view right after the launch screen is required to be presented in the device's orientation but should not autorotate to other orientations once it is presented. I used this code :
- (BOOL)shouldAutorotate {
[super shouldAutorotate];
return YES;
}
-(UIInterfaceOrientationMask) supportedInterfaceOrientations {
[super supportedInterfaceOrientations];
return UIInterfaceOrientationMaskAll;
}
This works well for iPad but in iPhone, setting NO for shouldAutorotate results in Portrait only. Is this normal behaviour? If so how can all orientations be supported without autorotation in iPhone, for a particular view controller only?
You need to detect what orientation the device is in when you start up:
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
Convert that to an interface orientation:
switch (orientation) {
case UIDeviceOrientationLandscapeRight:
return UIInterfaceOrientationLandscapeLeft;
default:
return UIInterfaceOrientationLandscapeRight;
}
Now store it somewhere for global access and return that:
- (UIInterfaceOrientationMask) supportedInterfaceOrientations {
return globalOrientation;
}
I have designed UI for my iOS app with wAny hRegular setting.
Now orientation works fine in iPad. But when app is running on iPhone, orientation gives a blank white screen.
Is there any way such that I can lock orientation in iPhones only and orientation is available for iPad?
You can use shouldAutorotate and supportedInterfaceOrientation UIViewController methods to lock the rotation on iPhone but not on iPad.
Simply return YES for iPad and NO for iPhone in shouldAutorotate, and you returns the allowed orientation for iPhone/iPad in supportedInterfaceOrientation.
Yes #david"mArm"Ansermot is right, I have just added the code, for quick snap:-
- (BOOL)shouldAutorotate {
UIDevice* thisDevice = [UIDevice currentDevice];
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
return YES;
}
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
UIDevice* thisDevice = [UIDevice currentDevice];
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
return (UIInterfaceOrientationMaskAll);
}
return (UIInterfaceOrientationMaskPortrait);
}
You can do this from project settings, please find the screenshots
1. http://i.imgur.com/efGrNcL.png "iPhone settings"
2. http://i.imgur.com/I25Z2cj.png "iPad settings"
My app works only in Landscape.
Thus I set the Initial Orientation and Supported Orientation at LandScape Home Right.
However, initial launch orientation always becomes Portrait.
After navigating to next page and return, the orientation is correct.
This might be the similar question to the following, In IOS 6 on iPad, initial rotation is always portrait, after that it always rotates correctly
But the solution there is not working as the 'handleRotationFor' gives warning:
instanceMethod -handleRotationFor is not found (return type defaults to 'id')
How should I fix this error?
Please help
project Targets --> click on "Summary" tab and choose your Orientation in "Supported interfaces Orientations"
In Xcode in the files pane click on your project and then select target from right pane. Then in Summary tab see supported orientations. Only select Landscape right. Also you can implement
-(NSUInteger)supportedInterfaceOrientation {
return UIInterfaceOrientationMaskLandscapeRight;
}
in your app delegate
In Xcode under summary tab make sure portrait and upside down are not pressed down. Also Add these three functions.
// *********************
// ios6 orientation support
// *********************
- (BOOL)shouldAutorotate
{
UIInterfaceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
return NO;
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
return NO;
}
else
{
return YES;
}
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
return NO;
}
else
{
return YES;
}
}
Check the supported orientations in the info.plist of the app. Check and set orientations in the nib file as well.
You can check orientations by using [[UIDevice currentDevice] orientation]
I set the initial orientation of the app to UIInterfaceOrientationLandscapeRight, but when I open the app with the physical orientation of UIInterfaceOrientationLandscapeLeft, it will show the interface of UIInterfaceOrientationLandscapeRight first, then rotate to the UIInterfaceOrientationLandscapeLeft orientation.
How to make it open with the same orientation as the device?
You have to instruct your UIViewController instances on which interface orientations are supported by implementing "shouldAutorotateToInterfaceOrientation" as follows:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
return YES;
} else {
return NO;
}
}