纵向

时间:2014-02-14 04:59:06

标签: ios objective-c ipad

我有一个全屏显示的视图。我希望所呈现的全屏视图仅限于肖像。

任何人都可以帮我限制只有一个视图作为肖像吗?它不应该转向风景。

2 个答案:

答案 0 :(得分:2)

您使用的代码取决于您要定位的iOS:

iOS 6 +

有一种名为supportedInterfaceOrientations的方法可以满足您的需求:

- (NSUInteger)supportedInterfaceOrientations
{
    //If you want to support landscape
    return UIInterfaceOrientationMaskAll;

    //If you don't
    return UIInterfaceOrientationMaskPortrait;
}

 - (BOOL)shouldAutorotate {
    return YES;
}

在每个视图控制器中放置相应的return语句。


iOS 5及之前版本:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return !(UIInterfaceOrientationIsLandscape(toInterfaceOrientation));
}

答案 1 :(得分:1)

如果您使用VC之间的模型,@ David的回答是正确的。 但是,如果您使用UINavigationController推/弹,事情会更复杂。

仅在设备旋转或根控制器更改后才会调用

- (NSUInteger)supportedInterfaceOrientations- (BOOL)shouldAutorotate。推/弹不会成功。


在iOS5和之前版本中,有一个API:

[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationPortrait];

但它被弃用了。 Apple将其设为私有。

您仍然可以使用objc runtime api在iOS 6+中调用它,例如:

objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), @(UIInterfaceOrientationPortrait));

[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait)
                            forKey:@"orientation"];

注意:它是私有API ,这可能会被App Store拒绝。


另一个棘手的方法是,只提出一个空的VC,然后在viewDidAppear中将其解雇,例如:

[self presentViewController:[UIViewController new]
              animated:NO
              completion:^{
                  [self dismissViewControllerAnimated:NO completion:nil];
              }];

这将调用- (NSUInteger)supportedInterfaceOrientations- (BOOL)shouldAutorotate


如果您对上述内容不满意。尝试制作自己的NavigationController,或 看看这个问题:Why can't I force landscape orientation when use UINavigationController?