iOS界面横屏竖屏随意切换

Posted 偶阵雨ss33

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS界面横屏竖屏随意切换相关的知识,希望对你有一定的参考价值。

 https://www.jianshu.com/p/ea1682e80003

 

先讲需求:

APP中所有界面支持竖屏,只有在一个界面,点击一个btn之后变成横屏,再点就是竖屏。
在网上找了一些方法,发现实现不了,遂问了一个做过这个功能的朋友,得到朋友的支持之后,顺利解决这一问题

 
在这里因为是要全局界面都是支持竖屏,其他的不支持,在Xcode里面的配置
 技术图片
技术图片
Xcode配置

然后就是在AppDelegate中添加属性和方法,
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
//是否强制横屏
@property (nonatomic, assign) BOOL isForceLandscape;
//是否强制竖屏
@property (nonatomic, assign) BOOL isForcePortrait;

@end

 

技术图片
添加属性

 
技术图片
在AppDelegate中添加方法 
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    if (self.isForceLandscape) {
        return UIInterfaceOrientationMaskLandscape;
    }else if (self.isForcePortrait){
        return UIInterfaceOrientationMaskPortrait;
    }else{
        return UIInterfaceOrientationMaskPortrait;
    }
}

 

一下是viewController中,即需要转换屏幕方向的.m文件的代码:

#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//     _currentOrient = [UIApplication  sharedApplication].statusBarOrientation;
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"转一转" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(changeFrame:) forControlEvents:UIControlEventTouchUpInside];
    button.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:button];
    
   NSLayoutConstraint * centerX = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
   NSLayoutConstraint * centerY = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
   NSLayoutConstraint * width = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100];
   NSLayoutConstraint * height =[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:44];
    
    [NSLayoutConstraint activateConstraints:@[centerX,centerY,width,height]];
    centerY.active = YES;
    centerX.active = YES;
    width.active = YES;
    height.active = YES;
    
}
// 允许自动旋转
-(BOOL)shouldAutorotate{
    return YES;
}
// 横屏时是否将状态栏隐藏
-(BOOL)prefersStatusBarHidden{
    return NO;
}
-(void)changeFrame:(UIButton *)btn{
    
    btn.selected = !btn.selected;
    
    if (btn.selected) {
        [self forceOrientationLandscapeWith:self];
    }else{
        [self forceOrientationPortraitWith:self];
    }
}
// 横屏 home键在右边
-(void)forceOrientationLandscapeWith:(UIViewController *)VC{
    
    AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.isForcePortrait=NO;
    appdelegate.isForceLandscape=YES;
    [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:VC.view.window];
    
    //强制翻转屏幕,Home键在右边。
    [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];
    //刷新
    [UIViewController attemptRotationToDeviceOrientation];
}
// 竖屏
- (void)forceOrientationPortraitWith:(UIViewController *)VC{
    
    AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.isForcePortrait=YES;
    appdelegate.isForceLandscape=NO;
    [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:VC.view.window];
    
    //强制翻转屏幕
    [[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
    //刷新
    [UIViewController attemptRotationToDeviceOrientation];
}

这样就实现了点击切换屏幕方向的需求,嘻嘻!!

 

以上是关于iOS界面横屏竖屏随意切换的主要内容,如果未能解决你的问题,请参考以下文章

Android 模拟器横屏竖屏切换设置

Mac 横屏 竖屏快捷键

android Activity 的生命周期 以及横屏竖屏切换时 Activity 的状态变化

IQKeyboardManager 横屏后 Toolbar 显示异常

Android 4.0 ,如何截屏,当竖屏时。

Android 布局旋转 横屏竖屏