iOS横竖屏通知有两种,一种监听设备横竖屏状态,另一种监听状态栏横竖屏状态。与布局有关一般使用第二种,因为如果一个viewcontroller不支持自动旋转,当设备由竖屏转横屏时依然会执行监听设备横竖屏的通知方法。
1.监听设备横竖屏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
- (void)orientChange:(NSNotification *)notification
UIDeviceOrientation orient = [UIDevice currentDevice].orientation;
4种状态:
UIDeviceOrientationPortrait
UIDeviceOrientationPortraitUpsideDown
UIDeviceOrientationLandscapeLeft
UIDeviceOrientationLandscapeRight
2.监听状态栏横竖屏状态
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
- (void)orientChange:(NSNotification *)notification
UIInterfaceOrientation interfaceOritation = [[UIApplication sharedApplication] statusBarOrientation];
4种状态:
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft