有时候,我们会需要在整个项目中,使某一个ViewController支持屏幕旋转,而其他的ViewController并不能自动旋转。这是一个很常见的需求,下面就屏幕旋转相关问题做个小结。
最多见的是,视屏播放中的横屏模式,点击全屏按钮,播放页面横屏最大化。使用 CGAffineTransformMakeRotation旋转操作配合动画即可。
-(void)fullScreenClick: (UIButton *)btn {
btn.selected = !btn.selected;
if (btn.selected) {
self.mySuperView = self.superview;
[UIView animateWithDuration:0.3 animations:^{
[[UIApplication sharedApplication].keyWindow addSubview:self];
self.transform = CGAffineTransformMakeRotation(M_PI / 2);
} completion:nil];
self.frame = self.bigFrame;
}else {
[self removeFromSuperview];
[self.mySuperView addSubview:self];
[UIView animateWithDuration:0.3 animations:^{
self.transform = CGAffineTransformMakeRotation(M_PI * 2);
} completion:nil];
self.frame = self.smallFrame;
}
}
强制带有导航条的整个页面旋转
- (void)changeScreent :(BOOL)toRight
{
CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
[UIView animateWithDuration:duration animations:^{
// 修改状态栏的方向及view的方向进而强制旋转屏幕
[[UIApplication sharedApplication] setStatusBarHidden:toRight];
self.navigationController.view.transform = toRight?CGAffineTransformMakeRotation(M_PI_2): CGAffineTransformIdentity;
if (toRight) {
self.navigationController.view.bounds = CGRectMake(self.navigationController.view.bounds.origin.x, self.navigationController.view.bounds.origin.y, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
}else{
self.navigationController.view.bounds = CGRectMake(self.navigationController.view.bounds.origin.x, self.navigationController.view.bounds.origin.y, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}
}];
}
注意状态栏无法强制旋转,在手机方向不变的情况下,所以,最好把状态栏隐藏掉,回复的时候再显示出来。不可使用self.view.frame.size.width self.view.frame.size.height,这样会出现第一次旋转出现上下部分白边的Bug,需要使用 [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height。
系统支持横屏顺序为以下几种,前面的会使后面的方法失效,优先级依次降低。
Paste_Image.png
首先, - (BOOL)shouldAutorotate 必须在 self.window.rootViewController 中才能有效果,而且每当手机发生旋转时,就会掉用 rootViewController 的 - (BOOL)shouldAutorotate 方法。
首先我们要保证工程设置为未勾选的状态才行。
Paste_Image.png
一般我们的rootViewController 都是UINavigationcontroller ,所有我们在UINavigationcontroller中设置如下方法 #获取栈最顶端的controller对旋转的支持状态即可, #然后在每一个VC中都要设置 - (BOOL)shouldAutorotate 来确定当前的VC是否支持横竖屏 # 如果支持,还需要设置 - (NSUInteger)supportedInterfaceOrientations//支持哪些方向
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
如果rootViewController 都是 tarBarController ,所有我们在tarBarController中设置如下方法
- (BOOL)shouldAutorotate{
return self.selectedViewController.shouldAutorotate;
}
这样的情况下,每个VC都是默认支持旋转的,那么我们需要在每个VC中都设置- (BOOL)shouldAutorotate吗?答案是否定的。我们可以创建一个controller的基类BaseViewController每个controller都继承BaseViewController ,在BaseViewController中重写- (BOOL)shouldAutorotate 方法中 return NO; 默认关闭,,在需要开启的子类中再次重写- (BOOL)shouldAutorotate 方法,在方法中return YES即可。
- (BOOL)shouldAutorotate
{
return YES;
}
# 点击全屏
- (IBAction)large:(id)sender
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
# 全屏返回
- (IBAction)largeBack:(id)sender
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
# 在这个方法中设置屏幕旋转时的 页面设置。
- (void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
self.navigationController.navigationBarHidden = NO;
self.toolBar.hidden = NO;
self.largeBackButton.hidden = YES;
self.bottomView.hidden = NO;
self.largeTitleLabel.hidden = YES;
self.localRecrodContraint.constant = 10;
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
self.navigationController.navigationBarHidden = YES;
self.localRecrodContraint.constant = 50;
self.toolBar.hidden = YES;
self.largeTitleLabel.hidden = NO;
self.largeBackButton.hidden = NO;
self.bottomView.hidden = YES;
}
}
******************更新****************
上面的方法完美解决了我一个工程的单页面屏幕旋转问题,可是在另一个工程中,上面的方法确出现了一个Bug,真的很蛋疼。
按照上面的方法我确实达到了,单页面旋转,其他页面不旋转的效果,但是有个问题: 在页面不旋转的情况下,状态栏确会随着手机的旋转而旋转,着实蛋疼。目前不清楚为什么一样的设置,在两个项目中效果不一样。
解决方法:
# AppDelegate 中
@property (strong, nonatomic) MyNavigationController* nav;
//手机方向发生变化时就会掉用
手机方向不发生变化时就不会掉用
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return self.nav.topViewController.supportedInterfaceOrientations;//支持所有方向
}
# MyNavigationController中
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
# BaseViewController中
- (BOOL)shouldAutorotate
{
return NO;
}
//不设置的话,虽然页面是没有旋转,但是状态栏会随着手机旋转而旋转。
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
#需要旋转的VC中
- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
可见,状态栏的方向跟页面支持的方向是一样的。所以页面支持的方向是一定要设定的,不设定的话就是默认的:左中右。
关于屏幕旋转的问题,目前先写这些,后续如果有新的东西收获,会更新上去的。