您的位置:首页 > 移动开发 > IOS开发

IOS横竖屏

2015-10-28 00:00 316 查看

1.横竖屏设定

1.1XCode选择设定(整个app有效)
选择Project->Target->General->Deployment Info->Device Orientation,勾选需要支持的屏幕方向,比如
:fa-square-o:Portrait
:fa-square-o:Upside Down
:fa-square-o:Landscape Left
:fa-square-o:Landscape Right

1.2.AppDelegate.m文件中代码设定(整个app有效)
在AppDelegate.m中添加方法:

//IOS6及以上
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  NS_AVAILABLE_IOS(6_0){
return UIInterfaceOrientationMaskPortrait;
}

1.3.单个View横竖屏设定(单个View有效)
主要依赖于以下几个函数

//当前viewcontroller是否支持转屏
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0);

//当前viewcontroller支持哪些转屏方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);

//当前viewcontroller默认的屏幕方向
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0);

2.相关(ENUM)UIInterfaceOrientation说明

UIInterfaceOrientationMask ->UIInterfaceOrientation的多种组合

UIInterfaceOrientationMaskPortrait  //竖屏
UIInterfaceOrientationMaskLandscapeLeft  //左横屏
UIInterfaceOrientationMaskLandscapeRight  //右横屏
UIInterfaceOrientationMaskPortraitUpsideDown  //竖屏(颠倒)
UIInterfaceOrientationMaskLandscape  //横屏
UIInterfaceOrientationMaskAll  //所有状态
UIInterfaceOrientationMaskAllButUpsideDown  //除颠倒竖屏外

3.转屏时触发哪些函数

<1>调用于翻转之前

//一般用来禁用某些控件或者停止某些正在进行的活动,比如停止视频播放。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration NS_DEPRECATED_IOS(2_0,8_0);

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);

<2>调用于翻转的过程中

//一般用来定制翻转后各个控件的位置、大小等。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration NS_DEPRECATED_IOS(3_0,8_0);

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);

<3>调用于整个翻转完成之后。

//一般用来重新启用某些控件或者继续翻转之前被暂停的活动,比如继续视频播放

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation NS_DEPRECATED_IOS(2_0,8_0);

4.判断当前屏幕状态(横/竖)

1. UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
2. UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS 横竖屏