我有两个 viewController,所以我希望第一个 viewController 可以旋转所有侧面,第二个 viewController 只能旋转左侧。这是我在第二个 viewController 中的代码。非常感谢。
-(NSUInteger) supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
- (BOOL)shouldRotateToOrientation:(UIDeviceOrientation)orientation {
if (orientation == UIDeviceOrientationLandscapeLeft) {
return NO;
}
else if (orientation == UIDeviceOrientationLandscapeRight) {
return NO;
}
else if (orientation == UIDeviceOrientationPortrait) {
return NO;
}
else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
return YES;
}
}
//使用此方法检查将执行哪个方向,如果您//想要停止方向,则可以返回no,如果您返回yes,则方向执行
首先需要添加 NavigationController 的类别
在 AppDelegate 中声明该 bool 以在整个项目中使用。
声明属性 AppDelegate.h
@property BOOL * islandscape;
AppDelegate.m 文件
@synthesize 岛景;
.h 文件
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
@interface UINavigationController (orientation1)
@end
.M 文件
#import "UINavigationController+orientation1.h"
@implementation UINavigationController (orientation1)
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
if (delegate.islandscape)
{
// for iPhone, you could also return UIInterfaceOrientationMaskAllButUpsideDown
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
@end
然后在 SeconViewController 中使用该 bool 变量。
delegate.islandscape=Yes;
如果您使用的是navigationController,请创建一个这样的类别,
@interface UINavigationController (Rotation_IOS6)
@end
@implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate
{
if([self.visibleViewController isMemberOfClass:NSClassFromString(@"SecondViewController")])
{
return UIInterfaceOrientationMaskLandscapeLeft
}
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return [[self topViewController] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
if([self.visibleViewController isMemberOfClass:NSClassFromString(@"SecondViewController")])
{
return UIInterfaceOrientationMaskLandscapeLeft
}
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end