我只想在我的 UINavigationController 堆栈中的一个视图上支持不同的方向。我怎样才能做到这一点?
它还必须在 iOS5 中工作。
我只想在我的 UINavigationController 堆栈中的一个视图上支持不同的方向。我怎样才能做到这一点?
它还必须在 iOS5 中工作。
我在 iOS6 如何处理方向方面遇到了很多麻烦,希望这就是你要找的。
创建一个 UINavigationController 类别并将其命名为“UINavigationController+autoRotate”。
把它放在你的 UINavigationController+autoRotate.h 中:
#import <UIKit/UIKit.h>
@interface UINavigationController (autoRotate)
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
-(BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;
@end
把它放在 UINavigationController+autoRotate.m 中:
#import "UINavigationController+autoRotate.h"
@implementation UINavigationController (autoRotate)
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
- (BOOL)shouldAutorotate
{
return [self.visibleViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
if (![[self.viewControllers lastObject] isKindOfClass:NSClassFromString(@"ViewController")])
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
else
{
return [self.topViewController supportedInterfaceOrientations];
}
}
@end
对于您不想旋转的视图,添加:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
return NO;
}
对于您想要旋转的视图:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIDeviceOrientationPortraitUpsideDown);
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (BOOL)shouldAutorotate
{
return YES;
}
在您的应用程序的委托中,添加:
- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
我建议您不要在 UINavigationController 上创建类别来覆盖这些方法。类别的目的不是为了做到这一点,并且不能保证您的代码将被加载而不是 Apple 的代码(即使确实有效)。我建议你创建一个 UINavigationController 的子类,并覆盖其中的那些方法。
在以下情况下,该解决方案不适用于 iOS 6(在 iOS 5 上正常):
vc A
仅支持纵向vc B
支持所有方向vc B
我们从推vc A
,旋转vc B
(例如横向)并弹回vc A
。vc A
方向保持在横向模式...