在iOS 8之前,我们将下面的代码与supportedInterfaceOrientations和shouldAutoRotate委托方法结合使用,以强制应用程序方向指向任何特定方向.我使用下面的代码段以编程方式将应用程序旋转到所需的方向.首先,我正在改变状态栏方向.然后只显示并立即关闭模态视图会将视图旋转到所需的方向.
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
UIViewController *c = [[UIViewController alloc]init];
[self presentViewController:vc animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
Run Code Online (Sandbox Code Playgroud)
但这在iOS 8中失败了.而且,我已经看到堆栈溢出的一些答案,人们建议我们应该始终从iOS 8开始避免这种方法.
更具体地说,我的应用程序是一种通用类型的应用程序.总共有三个控制器.
First View控制器 - 它应该支持iPad中的所有方向,并且只支持iPhone中的纵向(主页按钮).
第二视图控制器 - 它应该只支持所有条件下的横向
第三视图控制器 - 它应该只支持所有条件下的风景
我们使用导航控制器进行页面导航.从第一个视图控制器,在按钮单击操作上,我们在堆栈上推送第二个.因此,当第二个视图控制器到达时,无论设备方向如何,应用程序都应仅锁定横向.
下面是我shouldAutorotate和supportedInterfaceOrientations第二和第三视图控制器中的方法.
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeRight;
}
-(BOOL)shouldAutorotate {
return NO;
}
Run Code Online (Sandbox Code Playgroud)
对于这种或任何更好的方法来锁定iOS 8的特定方向的视图控制器是否有任何解决方案.请帮忙!!
我有一个iPhone应用程序,其中大多数只是纵向的,但有一个视图控制器(视频播放器屏幕),必须支持纵向和横向(这仅适用于iOS 8).为了达到这个目的,我已经设置了app的plist来支持肖像和两种风景,然后是子类UINavigationController; 在这个子类中我重写
- (BOOL)shouldAutorotate {
return YES;
}
Run Code Online (Sandbox Code Playgroud)
和
- (NSUInteger)supportedInterfaceOrientations {
if ([self.visibleViewController isKindOfClass:[MyVideoPlayerScreen class]]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
Run Code Online (Sandbox Code Playgroud)
这大部分都按预期工作:初始屏幕都是纵向的,即使设备转为横向也会保持纵向.视频播放器最初呈现时:
MyVideoPlayerScreen *newVC = [MyVideoPlayerScreen new];
[self.navigationController pushViewController:newVC animated:YES];
Run Code Online (Sandbox Code Playgroud)
也是纵向的,但是当设备转动时它会旋转到横向 - 到目前为止一切都很好.
问题是,如果我将设备转为横向,视频播放器也会出现景观,但是当我通过后退按钮关闭视频播放器屏幕时,底层视图控制器(应该是仅限肖像的)现在是也在景观中.如果我将设备旋转回纵向,则视图控制器也会旋转回纵向,然后从该点开始正确锁定设备.
如何弹出原始视图控制器(应该是仅限肖像),以便在其上方的横向视图控制器弹出时自动返回到纵向?
这个问题已被问过一百万次,但似乎已发布的修复程序都是在iOS 8中无法运行的黑客攻击.
更新:我发现了一个"类型"修复程序,可以在iOS 8中运行.在我的UINavigationController子类中,我处理<UINavigationControllerDelegate>协议.我实现了这个方法:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (![viewController isKindOfClass:[MyVideoPlayerScreen class]]) {
// if the current orientation is not already portrait, we need this hack in …Run Code Online (Sandbox Code Playgroud) 我只想在我的UINavigationController堆栈中的一个View上支持不同的Orientations.我怎样才能做到这一点?
它也必须在iOS5中工作.
objective-c orientation uiviewcontroller uinavigationcontroller ios6