先講需求:
APP中所有界面支持豎屏,只有在一個界面,點擊一個btn之后變成橫屏,再點就是豎屏。
在網(wǎng)上找了一些方法,發(fā)現(xiàn)實現(xiàn)不了,遂問了一個做過這個功能的朋友,得到朋友的支持之后,順利解決這一問題,我這里將demo附上。
首先,看效果圖:

點擊橫屏.gif
在這里因為是要全局界面都是支持豎屏,其他的不支持,在Xcode里面的配置

Xcode配置
然后就是在AppDelegate中添加屬性和方法,

添加屬性

在AppDelegate中添加方法
一下是viewController中,即需要轉(zhuǎn)換屏幕方向的.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:@"轉(zhuǎn)一轉(zhuǎn)" 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;
}
// 允許自動旋轉(zhuǎn)
-(BOOL)shouldAutorotate{
return YES;
}
// 橫屏?xí)r是否將狀態(tài)欄隱藏
-(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];
//強制翻轉(zhuǎn)屏幕,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];
//強制翻轉(zhuǎn)屏幕
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
//刷新
[UIViewController attemptRotationToDeviceOrientation];
}