iOS 旋转屏的问题
2018-07-20 本文已影响0人
Clark_new
首先设置旋转屏

模态视图方法:
- (BOOL)shouldAutorotate;
- (UIInterfaceOrientationMask)supportedInterfaceOrientations;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
第一个方法决定是否支持多方向旋转屏,如果返回NO则后面的两个方法都不会再被调用,而且只会支持默认的UIInterfaceOrientationMaskPortrait方向;
第二个方法直接返回支持的旋转方向,该方法在iPad上的默认返回值是UIInterfaceOrientationMaskAll,iPhone上的默认返回值是UIInterfaceOrientationMaskAllButUpsideDown,官方文档有说明
第三个方法返回最优先显示的屏幕方向,比如同时支持Portrait和Landscape方向,但想优先显示Landscape方向,那软件启动的时候就会先显示Landscape,在手机切换旋转方向的时候仍然可以在Portrait和Landscape之间切换
在UINavigationController或者自定义的UINavigationController中重写上面三个方法,即可实现在特定的控制器实现屏幕旋转代码如下:
- (BOOL)shouldAutorotate{
// 返回当前显示的viewController是否支持旋转
return [self.visibleViewController shouldAutorotate];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
// 返回当前显示的viewController支持旋转的方向
return [self.visibleViewController preferredInterfaceOrientationForPresentation];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
// 返回当前显示的viewController是优先旋转的方向
if (![self.visibleViewController isKindOfClass:[UIAlertController class]]) {//iOS9 UIWebRotatingAlertController
return [self.visibleViewController supportedInterfaceOrientations];
}else{
return UIInterfaceOrientationMaskPortrait;
}
}
然后在想要旋转屏幕的的控制器中重写上面三个方法:
- (BOOL)shouldAutorotate {
return YES;
}
// 这里返回需要支持旋转的方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeLeft;
}
// 优先显示的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
push控制器方法
//强制转屏
- (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;
// 从2开始是因为0 1 两个参数已经被selector和target占用
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
或者也可以这么做
在AppDelegate中添加方法关闭横竖屏切换,方法如下
1.AppDelegate.h中外露一个属性
@property(nonatomic,assign)BOOL allowRotation;//是否允许转向
2.AppDelegate.m中添加方法(如果属性值为YES,仅允许屏幕向左旋转,否则仅允许竖屏)
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
{
if (_allowRotation == YES) {
return UIInterfaceOrientationMaskLandscapeLeft;
}else{
return (UIInterfaceOrientationMaskPortrait);
}
}
1.在需要强制横屏的控制器.m中添加旋转为横屏方法
- (void)setNewOrientation:(BOOL)fullscreen
{
if (fullscreen) {
NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}else{
NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
}
2.view DidLoad中添加以下代码
AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = YES;//(以上2行代码,可以理解为打开横屏开关)
[self setNewOrientation:YES];//调用转屏代码
3.重写导航栏返回箭头按钮,拿到返回按钮点击事件
- (void)back
{
AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = NO;//关闭横屏仅允许竖屏
[self setNewOrientation:NO];
[self.navigationController popViewControllerAnimated:YES];
}