ViewDidload中的以编程方式设置方向无法正常工作
-
23-12-2019 - |
题
我为iPad创建了一个应用程序,其中象限应该仅以纵向模式显示。所以我在ViewDidload中使用了以下代码。
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
.
但它不起作用。我使用iOS 7与Xcode 5.如果我从横向模式打开我的应用,它会自动转向纵向模式。但我是这样的:
但它应该是这样的:
可以帮助我解决这个问题。 提前致谢。
解决方案
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
.
此方法已不推荐使用。您无法使用此方法了。
https://stackoverflow.com/a/12813644/1405008
上面的链接详细介绍了如何为世代odicetagcode提供肖像模式。
如果你被推入世纪odicetagcode,然后尝试这个。
https://stackoverflow.com/a/16152506/14152506/1405008/1405008
其他提示
以下代码即将从纵向转换到纵向横向的导航控制器视图:
[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;
}
.
只是希望为你提供一些其他想法。
在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;
}
}
.
根据您的要求设置BOOL
来自屏幕截图,我可以看到您的应用程序是标签基础。几天前,我面临着同样的问题。我解决了它。我问一个几乎相同的问题。然后在几个小时的阅读后,我解决了这个问题。有关我的问题和答案 。
有很多类似的问题和答案,但不知何故,他们都没有为我工作,因为我需要允许在iPhone上的纵向模式和只有横向模式ipad ,所以共享我的解决方案:
-
删除与设备方向(
shouldAutoRotate, supportedInterfaceOrientations, preferredInterfaceOrientations
等)相关的所有方法,因为它们可能与彼此冲突 -
在项目设置中启用所有4模式:纵向,颠倒,左侧横向,横向右侧
-
选择识别设备类型(iPhone或iPad)的方法。我正在使用
UIScreen.mainScreen().bounds.height which
并不理想,而是适合我的需求 -
在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; } }
在Xcode 7.1.1上进行测试.1所有运行IOS9.1 的iPhone和iPad模拟器