[英]Enable portrait and landscape for one view controller and only portrait for other view controllers in swift?
[英]Maintain one view controller in Portrait and other in Landscape mode iOS
我有一个带有2个视图控制器的iOS应用,即FirstViewController
和SecondViewController
。 我窗口的rootViewController是UINavigationController 。
FirstViewController应该仅在纵向模式下工作,而SecondViewController仅在横向模式下工作。
搜索整个Stackoverflow,我发现对于iOS6及更高版本,我必须在UINavigationController
上创建一个类别并覆盖-supportedInterfaceOrientations
问题
从FirstViewController开始。 现在我的手机处于纵向模式,我按SecondViewController,视图以纵向模式加载。 旋转手机使其处于横向状态后,视图将旋转为横向(从此以后,将不再返回纵向)。
当我弹出时,FirstViewController将再次处于Portrait(无论手机的方向如何)。
我希望SecondViewController根本不以纵向模式显示。 我整日绞尽脑汁...找不到解决方案。
评判
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *vc = [[ViewController alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
[self.navigationController setNavigationBarHidden:YES];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
FirstViewController
- (IBAction)btnClicked:(id)sender
{
SecondViewController *vc = [[SecondViewController alloc] init];
[self.navigationController pushViewController:vc animated:NO];
}
#pragma mark - Rotation handlers
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
SecondViewController
- (IBAction)btnClicked:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Rotation handlers
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
UINavigation类别
@implementation UINavigationController (Rotation)
-(BOOL)shouldAutorotate
{
//return [self.topViewController shouldAutorotate];
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
@end
好吧,有点晚了,但这就是我的想法。
虽然有很多解决方案可用于FirstVC为Portrait且SecondVC可以为Portrait and Landscape的情况,但我找不到解决此问题的任何好方法(仅First Portrait和Second Landscape)。 这是我所做的:
将两个视图控制器都嵌入其自己的导航控制器中。 创建两个新类,例如FirstNavController和SecondNavController子类化UINavigationController。 将它们用作导航控制器。 (如果使用的是StoryBoards,请选择导航控制器,转到Identity Inspector并更改“ Class”字段)。
现在,在FirstNavController中,添加:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
在SecondNavController中:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
您将必须模态呈现SecondNavController。
您的View Controller中无需执行任何操作。 确保在应用程序设置中添加了所有必需的方向。
这种方法的唯一缺点是,两个视图都不在同一个导航堆栈中,因为第二个视图是模态显示的,所以您不会看到后退按钮。 但是您可以自己添加一个取消/关闭按钮,然后在SecondVC中调用dismissViewControllerAnimated。
在我以前的应用程序中,我已经做到了
首先,您需要启用纵向,横向向左,横向向右定向
现在
将以下代码设置为您的FirstViewController
-(void) viewWillAppear:(BOOL)animated{
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
}
- (BOOL)shouldAutorotate
{
return NO;
}
将下面的代码设置为您的secondViewController
#define degreesToRadian(x) (M_PI * (x) / 180.0)
@implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
@end
@interface secondViewController ()
@end
@implementation secondViewController
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
@end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.