IOS 屏幕旋转
需要哪个viewController支持旋转,就要重写旋转方法。
在IOS 6.0之前 只需要重写
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);
但IOS 6.0 之后要重写两个方法了
- (BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;
如果是含有UINavigationController、UITabBarController的话需要在分类中重写支持旋转的方法。
注意,如果没有指定根视图控制器,只要 addSubView 了,这样是不起作用的。
-(void)deviceOrientation:(NSNotification *)notification{ UIDevice *device = [notification object]; //设备的朝向,这个方法一直执行 NSLog(@"%D",device.orientation); } //ios 6.0之后的方法 屏幕旋转 分拆为两个方法 -(BOOL)shouldAutorotate{ NSLog(@"%s",__func__); return YES;//支持旋转返回YES } //支持的方向 -(NSUInteger)supportedInterfaceOrientations{ NSLog(@"%s",__func__); /* typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) { UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait), UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft), UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight), UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown), UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown), UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), }; */ return UIInterfaceOrientationMaskPortrait; } // 5.0之前使用的方法 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ /* typedef NS_ENUM(NSInteger, UIInterfaceOrientation) { UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight, UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft }; */ NSLog(@"%s",__func__); //表示除了这个方向,其他方向都支持,如果只支持某一方向写 == return (toInterfaceOrientation != UIInterfaceOrientationMaskAllButUpsideDown); } //当设置旋转到不支持的方向不会调用这个方法 -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ //在这里设置不同方向时屏幕内显示的位置,一般改frame NSLog(@"%s",__func__); }