ios键盘横屏_iOS界面横屏竖屏随意切换

该博客围绕APP单界面横竖屏切换需求展开。APP全局界面支持竖屏,仅一个界面点击按钮可切换横竖屏。博主经朋友支持解决问题并附上demo,介绍了Xcode配置,还给出AppDelegate和ViewController中相关代码,实现点击切换屏幕方向。

先讲需求:

APP中所有界面支持竖屏,只有在一个界面,点击一个btn之后变成横屏,再点就是竖屏。

在网上找了一些方法,发现实现不了,遂问了一个做过这个功能的朋友,得到朋友的支持之后,顺利解决这一问题,我这里将demo附上。

首先,看效果图:

点击横屏.gif

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

Xcode配置

然后就是在AppDelegate中添加属性和方法,

添加属性

在AppDelegate中添加方法

一下是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];

}

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值