以编程方式在viewdidload中设置方向不起作用

时间:2014-02-27 05:37:17

标签: ios screen-orientation device-orientation

我为ipad创建了一个应用程序,其中主屏幕应仅以纵向模式显示。所以我在viewdidload中使用了以下代码。

[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];

但它不起作用。我正在使用带有xcode 5的ios 7.如果我从横向模式打开我的应用程序,它会自动转为纵向模式。但我会这样:

enter image description here

但它应该是这样的:

enter image description here

任何人都可以帮我解决这个问题。 提前谢谢。

5 个答案:

答案 0 :(得分:2)

以下代码将以编程方式将navigationController视图从Portrait转换为Landscape:

[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;
}

希望能为您提供其他一些想法。

答案 1 :(得分:0)

在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

答案 2 :(得分:0)

[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];

不推荐使用此方法。你不能再使用这种方法了。

https://stackoverflow.com/a/12813644/1405008

以上链接详细介绍了如何为UIViewController提供仅限肖像模式。

如果你进入NavigationViewController然后试试这个。

https://stackoverflow.com/a/16152506/1405008

答案 3 :(得分:0)

从您的屏幕截图中我可以看到您的应用是标签库。几天前我遇到了同样的问题。我解决了我问了一个与你几乎相同的问题。经过几个小时的阅读后,我解决了这个问题。有question and answer的链接。

答案 4 :(得分:0)

在SO上有很多类似的问题和答案,但不知何故它们都不适合我,因为我需要在iPhone上允许仅限肖像模式,并且仅允许横向模式iPad ,分享我的解决方案:

  1. 删除与设备方向相关的所有方法(shouldAutoRotate, supportedInterfaceOrientations, preferredInterfaceOrientations等),因为它们可能相互冲突

  2. 在项目设置中启用所有4种模式:人像,上下颠倒,左侧风景,右侧风景

  3. 选择识别设备类型(iPhone或iPad)的方法。我使用的UIScreen.mainScreen().bounds.height which并不理想但适合我的需求

  4. 将以下内容放在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;
        }
    }
    
  5. 在Xcode 7.1.1上测试了所有运行iOS9.1的iPhone和iPad模拟器