Ios 在纵向模式下维护一个视图控制器,在横向模式下维护另一个视图控制器
我有一个iOS应用程序,有两个视图控制器,即-Ios 在纵向模式下维护一个视图控制器,在横向模式下维护另一个视图控制器,ios,iphone,objective-c,Ios,Iphone,Objective C,我有一个iOS应用程序,有两个视图控制器,即-FirstViewController和SecondViewController我的窗口的rootViewController是UINavigationController FirstViewController应仅在纵向模式下工作,而SecondViewController应仅在横向模式下工作 在Stackoverflow上搜索,我发现对于iOS6及以上版本,我必须在UINavigationController上创建一个类别,并覆盖-支持的接口方向
FirstViewController
和SecondViewController
我的窗口的rootViewController是UINavigationController
FirstViewController应仅在纵向模式下工作,而SecondViewController应仅在横向模式下工作
在Stackoverflow上搜索,我发现对于iOS6及以上版本,我必须在UINavigationController
上创建一个类别,并覆盖-支持的接口方向
问题
从FirstViewController开始。现在我的手机处于纵向模式,我按下SecondViewController,视图以纵向模式加载。一旦我将手机旋转到横向视图,视图将旋转到横向视图(从这一点开始,将根本不会返回到纵向视图)
当我弹回来时,FirstViewController将再次处于纵向(无论手机的方向如何)
我希望SecondViewController不应该以纵向模式显示。我整天绞尽脑汁……找不到解决办法
APPDELEGATE
- (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;
}
- (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;
}
SecondViewController
- (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;
}
- (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;
}
u导航类别
@implementation UINavigationController (Rotation)
-(BOOL)shouldAutorotate
{
//return [self.topViewController shouldAutorotate];
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
@end
在我以前的应用程序中,我已经完成了 首先,您需要启用纵向、横向左侧和横向右侧方向来投影 现在 将以下代码设置为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
嗯,有点晚了,但我是这么想的 当FirstVC是纵向的,Second可以是纵向的和横向的时,有很多解决方案,但是我找不到解决这个问题的好方法(仅第一个纵向和第二个横向)。以下是我所做的: 将两个视图控制器嵌入各自的导航控制器中。创建两个新类,比如第一个NavController和第二个NavController子类UINavigationController。将它们用作导航控制器。(如果您使用的是故事板,请选择导航控制器,转到Identity Inspector并更改“类”字段) 现在,在FirstNavController中,添加:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
在第二个导航控制器中:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
您必须以模态方式呈现第二个NAVcontroller
无需在视图控制器中执行任何操作。确保在应用程序设置中添加所有必需的方向
这种方法的唯一缺点是,两个视图不在同一个导航堆栈中,因为第二个视图是以模式显示的,所以您不会看到“后退”按钮。但是,您可以自己添加一个取消/取消按钮,并在SecondVC中调用dismissViewControllerAnimated。通过使用这样的转换,我只是在愚弄系统,使其相信它处于横向模式。如果我显示一个UIAlertView,它仍将处于纵向模式…如果我们使用导航控制器,这能工作吗?我不这么认为,因为单个视图上的仿射变换不会变换导航栏、状态栏等。通过使用上述代码,所有工作正常。在横向模式下,视图将显示。检查此屏幕截图,遮罩特定viewController上的方向。