只在特定的页面支持屏幕旋转
- 最近做项目还是遇到了一些问题,比如说只在一个播放视频的页面支持旋转,其他页面不支持。看了网上好多的方法,大部分都没用,有的还写的那么复杂,结果屁用没有。
- 这个是通用的,大家都这么写(我自定义的tabbar和navgation结果没有用,继续往下看,最后一个可以)
- 首先在UINavigationController里面设置那些界面可以旋转
- //允许旋转
- (BOOL)shouldAutorotate
{
return YES;
}
/**
- (BOOL)shouldAutorotate
- topviewController就是当前导航控制器所控制的viewcontrol
*/
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
if ([self.topViewController isKindOfClass:[ViewController class]])
{
return self.topViewController.supportedInterfaceOrientations;
}
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
//在需要旋转的界面中写方法
- (void)viewWillAppear:(BOOL)animated{
[self interfaceOrientation:UIInterfaceOrientationLandscapeLeft];
}
(void)viewDidDisappear:(BOOL)animated{
[self interfaceOrientation:UIInterfaceOrientationPortrait];
}(void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
//强制转换
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@”setOrientation:”);
NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = orientation;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}或者强制横屏 (这个谁都会,但是不适用,往下看)
-
- (void)loadView{
self.view = [[UIView alloc]init];
CGRect frame = [UIScreen mainScreen].bounds;
AppDelegate app = (AppDelegate )[UIApplication sharedApplication].delegate;
UIDeviceOrientation duration = [[UIDevice currentDevice]orientation];
if (duration == UIDeviceOrientationLandscapeLeft) {
app.window.transform = CGAffineTransformMakeRotation(M_PI*0.5); [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:YES];
}else{
app.window.transform = CGAffineTransformMakeRotation(M_PI*1.5); [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:YES];
}
app.window.bounds = CGRectMake(0, 0, frame.size.height, frame.size.width);
}
- 其实就是这么简单 (自定义tabbar 和nav 完全好用,当时心里一万头草泥马路过)
-
- (UIInterfaceOrientationMask)application:(UIApplication )application supportedInterfaceOrientationsForWindow:(UIWindow )window
{
if (_allowRotation == 1) {
return UIInterfaceOrientationMaskAll;
}else{
return (UIInterfaceOrientationMaskPortrait);
}
}
- (UIInterfaceOrientationMask)application:(UIApplication )application supportedInterfaceOrientationsForWindow:(UIWindow )window
// 支持设备自动旋转
- (BOOL)shouldAutorotate
{
if (_allowRotation == 1) {
return YES;
}
return NO;
}
然后在你需要横屏的页面 allowRotation=1;就可以了