如何在iOS中强制视图进入风景? [英] How to force view to enter landscape in iOS?
问题描述
我在SO上看到了十几个可能的相关问题,但似乎没有人重复:
I've see a dozen of possible related questions on SO, but no one seems duplicated:
我的应用是基于标签的应用.
My app is a tab-based application.
当前,当输入某些视图时,我可以将手机旋转到横向,以使视图进入横向.
Currently when entering certain views, I can rotate my phone to landscape to let my view enter landscape.
主要代码:
// In app delegate
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (_allowRotation == YES) {
return UIInterfaceOrientationMaskLandscape;
}
else {
return UIInterfaceOrientationMaskPortrait;
}
}
在我想成为风景的视图中,将allowRotation
设置为是".
In views that I want to be landscape, I set allowRotation
to YES.
但是我想强迫视图进入风景.
换句话说,在输入某些视图时,它会自动进入风景,而无需旋转手机.甚至用户也将屏幕锁定为纵向.如何实现?
In other word, when entering certain views, it automatically enters landscape, no need to rotate phone. Even user locks the screen as portrait. How to achieve this?
我正在使用iOS 10.
I'm using iOS 10.
推荐答案
您必须覆盖要移动到横向的特定视图的onDidLoad或onDidAppear,例如,我将其添加到viewWillAppear和Disappear中:>
You have to override the onDidLoad or onDidAppear of the specific view that you want to move it to landscape, for example i added it to viewWillAppear and Disappear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.navigationController.navigationBarHidden = NO;
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
-(void)viewWillDisappear:(BOOL)animated {
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[super viewWillDisappear:animated];
}
所以我们看到的视图是应用程序移动到横向,而当移动消失时,它又回到纵向.对于appDelegate,您必须添加以下代码:
so we the view appear the application move to landscape and when the move disappear it come again to portrait. and for the appDelegate you have to add this:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
if ([navigationController.viewControllers.lastObject isKindOfClass:[OpencvViewController class]])
{
return UIInterfaceOrientationMaskLandscape;
}
else
return UIInterfaceOrientationMaskPortrait;
}
这篇关于如何在iOS中强制视图进入风景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!