2014-02-27 96 views
0

我已经创建了一个ipad应用程序,其中主屏幕只能在肖像模式下显示。所以我在viewdidload中使用了下面的代码。以编程方式在viewdidload中设置方向不起作用

[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait]; 

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

enter image description here

,但它应该是这样的:

enter image description here

任何一个可以帮我解决这个问题。 在此先感谢。

+0

的上面的方法被删除,现在不能使用。 –

回答

0
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait]; 

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此方法已被弃用。你不能再使用这种方法。

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

上面的链接提供了有关如何为UIViewController提供肖像唯一模式的细节。

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

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

+0

我检查了链接,它将整个应用限制为肖像模式。但我只需要将我的主屏幕限制为肖像模式。其他屏幕应该可以在两种模式下工作。 – gunas

+0

http://stackoverflow.com/a/12813644/1405008检查此链接 – CoolMonster

0

复制该代码在您的viewDidLoad中根据您的要求

0

从你的截图,我可以看到自己的应用标签基地。几天前我面临同样的问题。我解决了它。我问了一个和你的问题几乎相同的问题。然后在几个小时的阅读后,我解决了这个问题。有链接到我的question and answer

2

下面的代码是即将从肖像变换navigationController以景观编程:

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

只是希望给你提供一些其他的想法。

0

有很多关于这样类似的问题和答案,但不知何故,他们没有工作对我来说,因为我需要让仅适用于iPhone肖像模式,只有横向模式在iPad上,所以分享我的解决方案:

  1. 删除,因为它们可能互相

  2. 在项目设置发生冲突与设备方向(shouldAutoRotate, supportedInterfaceOrientations, preferredInterfaceOrientations等),所有的方法使所有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; 
        } 
    } 
    

测试上的Xcode 7.1.1所有的iPhone和iPad模拟器运行iOS9.1

相关问题