现在随着手机屏幕的增大,很多项目都是竖屏控制的,但是,也有需要横屏的时候,现在将方法整理一下。 1 项目中General 设置,图1是iOS7之前的版本,图2是iOS7之后的版本。 2 代码枚举值 UIDeviceOrientationUnknown, UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the topUIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left UIDeviceOrientationFaceUp, // Device oriented flat, face up UIDeviceOrientationFaceDown // Device oriented flat, face down */ 3 控制旋转的方法 -(BOOL)shouldAutorotate { // return NO;//不允许随着设备旋转 return YES; } -(NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskAllButUpsideDown; // 直接返回支持的旋转方向,该方法在iPad上的默认返回值是UIInterfaceOrientationMaskAll, //iPhone上的默认返回值是UIInterfaceOrientationMaskAllButUpsideDown } 4 项目实现方式 A. 强制横屏 选择了旋转屏幕View -(void)buildStageRotate { self.view.transform = CGAffineTransformMakeRotation(M_PI_2); } B.监测设备的变化 在- (void)viewDidLoad {}里面添加观察者 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; 实现效果 -(void)orientChange:(NSNotification *)notify { UIDeviceOrientation oriente = [UIDevice currentDevice].orientation; switch (oriente) { case UIDeviceOrientationPortrait: { NSLog(@"正常竖屏"); } break; case UIDeviceOrientationLandscapeLeft: { NSLog(@"home在右"); CGRect frame = _webShow.frame; frame.size.height = kUIHeight; frame.size.width = kUIWidth; _webShow.frame = frame; } break; case UIDeviceOrientationLandscapeRight: { NSLog(@"home在左"); } break; default: break; } } #define kUIHeight [[UIScreen mainScreen]bounds].size.height #define kUIWidth [[UIScreen mainScreen]bounds].size.width 旋转时候的大小设定不能使用 frame 要使用 bounds 其中subviews 一定要autoLayout,否则只是单纯的转屏,并没有实现横屏展示的效果 PS参考:后续看到非常详细的介绍 http://www.jb51.net/article/72826.htm ---------------------------------- -------------------------------- 2017-03-01 修改完善 UIInterfaceOrientation 旋转屏幕,利用状态条位置,进行页面的旋转 --- 首先设置,屏幕锁定 -(BOOL)shouldAutorotate{ return NO; } 其次,设置为点击按钮,屏幕竖屏和横屏进行切换 { NSLog(@"正常竖屏"); [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]; CGRect frame = self.showWeb.frame; frame.size.height = [[UIScreen mainScreen]bounds].size.height; frame.size.width = [[UIScreen mainScreen]bounds].size.width; self.showWeb.transform = CGAffineTransformIdentity; self.showWeb.frame = frame; NSLog(@"nomal frame is %@",NSStringFromCGRect(self.showWeb.frame)); } ~~~~~~~~~~~ NSLog(@"home在右,页面旋转"); [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]; CGRect frame = self.showWeb.frame; frame.size.height = [[UIScreen mainScreen]bounds].size.width; frame.size.width = [[UIScreen mainScreen]bounds].size.height; self.showWeb.transform = CGAffineTransformMakeRotation(M_PI/2);//旋转 self.showWeb.frame = frame; NSLog(@"right frame is %@",NSStringFromCGRect(self.showWeb.frame)); } ---------------------------- 旋转之后,状态栏是隐藏状态的,修改方式 1.在info.plist文件中,添加View controller-based status bar appearance,属性为bool,设为NO; 2 在application:didFinishLaunchingWithOptions:中添加以下下面代码: [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone]; ---------------------------- |
|