- (void)setOrientation: (UIInterfaceOrientation)orientation {
if([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation*invocation = [NSInvocationinvocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget: [UIDevice currentDevice]];
intval = orientation;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
在AppDelegate.h中定义一个变量:
BOOL allowRotation;
再定义一个宏:
#define ApplicationDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)
然后在AppDelegate.m文件中 实现允许屏幕的方向函数
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if(self.allowRotation) {
returnUIInterfaceOrientationMaskAll;
}
returnUIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate{
returnApplicationDelegate.allowRotation;
}
- (void)viewDidDisappear:(BOOL)animated{
[superviewDidDisappear:animated];
ApplicationDelegate.allowRotation==NO;
}
- (void)viewDidAppear:(BOOL)animated{
[superviewDidAppear:animated];
ApplicationDelegate.allowRotation==YES;
}
[[NSNotificationCenterdefaultCenter] addObserver:self selector:@selector(changeFrames) name:UIDeviceOrientationDidChangeNotification object:nil];