如何检测视图控制器的方向
How to detect orientation of View Controller
我有三个视图控制器,它们都被推送到我已经子类化的导航控制器上,以便只允许在第二个视图控制器中旋转,我这样做,
- (BOOL)shouldAutorotate
{
if ([self.topViewController isKindOfClass:[SecondViewController class]])
return YES;
return NO;
}
我在我的自定义导航控制器中编写了这段代码,问题是如果我以纵向模式打开我的应用程序然后将方向更改为横向模式我的视图控制器不会旋转但即使我的第二个视图控制器打开它以纵向模式打开,但我希望它以横向模式打开,因为它支持旋转。
我怎样才能做到这一点?
您需要在导航过程中使用 attemptRotationToDeviceOrientation
。您应该覆盖 push/pop 方法来调用 attemptRotationToDeviceOrientation
并有一个小的 UI 延迟 (dispatch_async
)
@implementation CustomNavigationController
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[super pushViewController:viewController animated:animated];
[self updateOrientaion];
}
- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated
{
[self updateOrientaion];
return [super popViewControllerAnimated:animated];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
if ([self.topViewController isKindOfClass:[SecondViewController class]])
return UIInterfaceOrientationMaskAll;
return UIInterfaceOrientationMaskPortrait;
}
- (void)updateOrientaion
{
dispatch_async(dispatch_get_main_queue(), ^{
[UIViewController attemptRotationToDeviceOrientation];
});
}
@end
但是当你弹出到 UINavigationController
的 rootViewController 时,supportedInterfaceOrientations
被调用为 rootViewController。所以你还需要为 FirstViewController
实现 supportedInterfaceOrientations
@implementation FirstViewController
.......
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
@end
我有三个视图控制器,它们都被推送到我已经子类化的导航控制器上,以便只允许在第二个视图控制器中旋转,我这样做,
- (BOOL)shouldAutorotate
{
if ([self.topViewController isKindOfClass:[SecondViewController class]])
return YES;
return NO;
}
我在我的自定义导航控制器中编写了这段代码,问题是如果我以纵向模式打开我的应用程序然后将方向更改为横向模式我的视图控制器不会旋转但即使我的第二个视图控制器打开它以纵向模式打开,但我希望它以横向模式打开,因为它支持旋转。
我怎样才能做到这一点?
您需要在导航过程中使用 attemptRotationToDeviceOrientation
。您应该覆盖 push/pop 方法来调用 attemptRotationToDeviceOrientation
并有一个小的 UI 延迟 (dispatch_async
)
@implementation CustomNavigationController
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[super pushViewController:viewController animated:animated];
[self updateOrientaion];
}
- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated
{
[self updateOrientaion];
return [super popViewControllerAnimated:animated];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
if ([self.topViewController isKindOfClass:[SecondViewController class]])
return UIInterfaceOrientationMaskAll;
return UIInterfaceOrientationMaskPortrait;
}
- (void)updateOrientaion
{
dispatch_async(dispatch_get_main_queue(), ^{
[UIViewController attemptRotationToDeviceOrientation];
});
}
@end
但是当你弹出到 UINavigationController
的 rootViewController 时,supportedInterfaceOrientations
被调用为 rootViewController。所以你还需要为 FirstViewController
supportedInterfaceOrientations
@implementation FirstViewController
.......
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
@end