问题
I have created an app for ipad in which the homescreen should be displayed only in portrait mode. So i have used the below code in viewdidload.
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
but it is not working. I am using ios 7 with xcode 5. If i open my app from landscape mode it shouuld automatically turn to portrait mode. but i am getting like this:
but it should be like this:
can any one help me to solve this issue. Thanks in advance.
回答1:
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
This method is deprecated. You cannot use this method anymore.
https://stackoverflow.com/a/12813644/1405008
Above link gives detail on how to provide portrait only mode for UIViewController
.
If you pushed into NavigationViewController
then try this.
https://stackoverflow.com/a/16152506/1405008
回答2:
Following code is about to transform a navigationController view from Portrait to Landscape programmatically:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
self.navigationController.view.transform = CGAffineTransformIdentity;
self.navigationController.view.transform = CGAffineTransformMakeRotation(M_PI*(90)/180.0);
self.navigationController.view.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
[UIView commitAnimations];
self.view.frame = self.view.frame; //This is necessary
self.wantsFullScreenLayout = YES;
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight;
}
Just hope to provide you some other ideas.
回答3:
copy this code in your viewdidload
UIInterfaceOrientation orientation = (UIInterfaceOrientation)[[UIDevice currentDevice] orientation];
if(orientation == UIInterfaceOrientationPortrait)
{
isLandscapeMode = NO;
inLandscapeRight = NO;
inLandscapeLeft = NO;
}
else if(orientation == UIInterfaceOrientationLandscapeRight)
{
isLandscapeMode = YES;
inLandscapeRight = YES;
inLandscapeLeft = NO;
} else if(orientation == UIInterfaceOrientationLandscapeLeft)
{
isLandscapeMode = YES;
inLandscapeRight = NO;
inLandscapeLeft = YES;
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
isLandscapeMode = YES;
inLandscapeLeft = YES;
inLandscapeRight = NO;
frameForProfile = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
isLandscapeMode = YES;
inLandscapeLeft = NO;
inLandscapeRight = YES;
frameForProfile = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}
else
{
isLandscapeMode = NO;
inLandscapeLeft = NO;
inLandscapeRight = NO;
}
}
set BOOL according to your requirement
回答4:
From you screenshots I can see that your app is tab base. Few days earlier I faced same problem. I solved it. I asked a question almost same to your. Then after a few hours of reading I solved the problem. There is the link to my question and answer.
回答5:
There are a lot of similar questions and answers on SO, but somehow none of them worked for me, as I needed to allow only portrait mode on iPhone, and only landscape mode on iPad, so sharing my solution:
Remove all methods related to device orientation (
shouldAutoRotate, supportedInterfaceOrientations, preferredInterfaceOrientations
, etc) as they might be conflicting with each otherIn project settings enable all 4 modes: Portrait, Upside Down, Landscape left, Landscape Right
Chose a method for identifying device type (iPhone or iPad). I am using
UIScreen.mainScreen().bounds.height which
is not ideal but suits my needsPlace the following in AppDelegate
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { if UIScreen.mainScreen().bounds.height < 760 { //iPhone UIApplication.sharedApplication().setStatusBarOrientation(.Portrait, animated: false); return UIInterfaceOrientationMask.Portrait; } else { UIApplication.sharedApplication().setStatusBarOrientation(.LandscapeLeft, animated: false); return UIInterfaceOrientationMask.Landscape; } }
Tested on Xcode 7.1.1 all iPhone and iPad simulators running iOS9.1
来源:https://stackoverflow.com/questions/22059913/programmatically-setting-orientation-in-viewdidload-is-not-working