首页 > 开发 > iOS > 正文

iOS 更改Device Orientation 如何对现有代码尽量少的影响

2017-09-08 13:45:20  来源:网友分享

公司项目2.x版本了, 目前有需求在某一页面横屏并且要用键盘输入;
之前 Device Orientation 只支持 竖屏,
现在要加上landscape right 了;
请问,如何做才能尽量不影响其他一定是竖屏的页面,

解决方案

通过修改单例对象属性到达横竖屏切换.
如使用AppDelegate, 在AppDelegate.h中添加一个属性

@property (nonatomic, assign) UIInterfaceOrientationMask orientationMask;

AppDelegate.m, 实现supportedInterfaceOrientationsForWindow方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // 设置默认值, 为竖屏    _orientationMask = UIInterfaceOrientationMaskPortrait;    return YES;}// window中支持的屏幕显示方向- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{    return _orientationMask;}

在横屏控制器中, 修改orientationMask的值

// 支持 横屏 竖屏[(AppDelegate *)[UIApplication sharedApplication].delegate setOrientationMask:UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight];

退出当前控制器时, 还原竖屏设置

// 支持 竖屏[(AppDelegate *)[UIApplication sharedApplication].delegate setOrientationMask:UIInterfaceOrientationMaskPortrait];

Demo链接
强制横屏Demo