UINavigationcontroller 中自动旋转 UIViecontroller 的问题
Issue in autorotating UIViecontrollers in UINavigationcontroller
我从 this tutorial 站点下载了一个关于在导航控制器中旋转视图控制器的示例。
该示例项目功能
- FirstviewController支持自拍
- 支持第二个视图控制器的所有方向(从第一个 VC 推送)
我需要的是,
- FirstviewController 应该支持所有方向
- 第二个视图控制器(从第一个 VC 推送)应该单独支持 potrait。
我所做的是,只是交换了视图控制器中的代码
在第一个视图控制器中
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
在第二个视图控制器和
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
但是,两个视图控制器都在旋转。
我该如何解决这个问题?有什么建议,请分享。
----编辑----
如果我在推送之前将模拟器旋转到横向,第二个 VC 也是横向视图。
如果我推到第二个vc,第二个VC是自拍模式(如果我在那里旋转它不旋转。)
我把日志放在 customNavigationcontroller NSLog(@"self.topViewController.class %@",self.topViewController.class);
。它只是在推送到第二个 vc 之后记录第一个视图控制器,而不是记录
我建议一个更简单的方法来解决这个问题。
Select 您项目的目标并像这样配置它:
然后转到 Appdelegate.m 并粘贴:
- (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
// Handling UITabBarController
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
}
// Handling UINavigationController
else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
}
// Handling Modal views
else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
}
// Handling UIViewController's added as subviews to some other views.
else
{
for (UIView *view in [rootViewController.view subviews])
{
id subViewController = [view nextResponder]; // Key property which most of us are unaware of / rarely use.
if ( subViewController && [subViewController isKindOfClass:[UIViewController class]])
{
return [self topViewControllerWithRootViewController:subViewController];
}
}
return rootViewController;
}
}
现在是有趣的部分。
把这个方法放在AppDelegate.m,你可以在这里决定每个控制器的方向
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
id presentedViewController = [self topViewController];
NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;
if (window && [className isEqualToString:@"FirstViewController"]) { //FirstviewController should support all orientations
return UIInterfaceOrientationMaskAll;
} else {
return UIInterfaceOrientationMaskPortrait; //Second view controller(pushed from first VC) should support potrait alone.
}
}
然后您可以删除您添加的所有其他方法,例如 shouldAutorotateToInterfaceOrientation、shouldAutorotate、supportedInterfaceOrientations、preferredInterfaceOrientationForPresentation。
这些方法现在都不需要了
-----编辑-----
只需使用
[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationPortrait] forKey:@"orientation"];
在我需要 potrait 的所有视图控制器中都可以解决问题。不需要其他东西。
我从 this tutorial 站点下载了一个关于在导航控制器中旋转视图控制器的示例。
该示例项目功能
- FirstviewController支持自拍
- 支持第二个视图控制器的所有方向(从第一个 VC 推送)
我需要的是,
- FirstviewController 应该支持所有方向
- 第二个视图控制器(从第一个 VC 推送)应该单独支持 potrait。
我所做的是,只是交换了视图控制器中的代码
在第一个视图控制器中
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
在第二个视图控制器和
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
但是,两个视图控制器都在旋转。
我该如何解决这个问题?有什么建议,请分享。
----编辑----
如果我在推送之前将模拟器旋转到横向,第二个 VC 也是横向视图。
如果我推到第二个vc,第二个VC是自拍模式(如果我在那里旋转它不旋转。)
我把日志放在 customNavigationcontroller NSLog(@"self.topViewController.class %@",self.topViewController.class);
。它只是在推送到第二个 vc 之后记录第一个视图控制器,而不是记录
我建议一个更简单的方法来解决这个问题。
Select 您项目的目标并像这样配置它:
然后转到 Appdelegate.m 并粘贴:
- (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
// Handling UITabBarController
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
}
// Handling UINavigationController
else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
}
// Handling Modal views
else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
}
// Handling UIViewController's added as subviews to some other views.
else
{
for (UIView *view in [rootViewController.view subviews])
{
id subViewController = [view nextResponder]; // Key property which most of us are unaware of / rarely use.
if ( subViewController && [subViewController isKindOfClass:[UIViewController class]])
{
return [self topViewControllerWithRootViewController:subViewController];
}
}
return rootViewController;
}
}
现在是有趣的部分。
把这个方法放在AppDelegate.m,你可以在这里决定每个控制器的方向
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
id presentedViewController = [self topViewController];
NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;
if (window && [className isEqualToString:@"FirstViewController"]) { //FirstviewController should support all orientations
return UIInterfaceOrientationMaskAll;
} else {
return UIInterfaceOrientationMaskPortrait; //Second view controller(pushed from first VC) should support potrait alone.
}
}
然后您可以删除您添加的所有其他方法,例如 shouldAutorotateToInterfaceOrientation、shouldAutorotate、supportedInterfaceOrientations、preferredInterfaceOrientationForPresentation。 这些方法现在都不需要了
-----编辑-----
只需使用
[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationPortrait] forKey:@"orientation"];
在我需要 potrait 的所有视图控制器中都可以解决问题。不需要其他东西。