UIDeviceOrientationDidChangeNotification和UIApplicationDidChangeStatusBarFrameNotification的區(qū)別
相同點:UIDeviceOrientationDidChangeNotification和UIApplicationDidChangeStatusBarFrameNotification一樣可以監(jiān)聽手機是否橫豎屏邦危。
區(qū)別: 當(dāng)全局禁掉自動旋轉(zhuǎn)屏幕的功能后府怯,手機旋轉(zhuǎn)UIApplicationDidChangeStatusBarFrameNotification不會再發(fā)出通知赘方。但UIDeviceOrientationDidChangeNotification會發(fā)出通知斑司,無論是否支持橫豎屏,都會發(fā)出通知昆稿。這樣可以方便的處理橫屏后的頁面顯示問題坊夫。
1.注冊通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeRotate:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
- (void)changeRotate:(NSNotification*)noti {
if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait
|| [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) {
//豎屏
NSLog(@"豎屏");
} else {
//橫屏
NSLog(@"橫屏");
}
}
2.兩者在監(jiān)聽的方向的多少有區(qū)別
UIApplicationDidChangeStatusBarFrameNotification常見的幾種情況:
UIDeviceOrientationPortrait
UIDeviceOrientationPortraitUpsideDown
UIDeviceOrientationLandscapeLeft
UIDeviceOrientationLandscapeRight
UIDeviceOrientationDidChangeNotification
typedef enum {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom
UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top
UIDeviceOrientationLandscapeLeft, // 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
} UIDeviceOrientation;
單頁面需要橫屏
//在UIApplication實現(xiàn)該方法
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if(self.isFull){
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
在需要支持橫屏的頁面修改isFull的值,并重寫方法
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeRotate:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.isFull = YES;
// 支持設(shè)備自動旋轉(zhuǎn)
- (BOOL)shouldAutorotate
{
return YES;
}
// 支持橫豎屏顯示
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
}