iOS横竖屏监控通知字段

本文对比了UIDeviceOrientationDidChangeNotification和UIApplicationDidChangeStatusBarFrameNotification两种iOS屏幕旋转通知的区别。前者即使在禁用自动旋转的情况下也会触发,后者则不会。此外,还介绍了如何监听这两种通知以及在特定页面上启用横屏的方法。
摘要由CSDN通过智能技术生成

UIDeviceOrientationDidChangeNotification和UIApplicationDidChangeStatusBarFrameNotification的区别

相同点:UIDeviceOrientationDidChangeNotification和UIApplicationDidChangeStatusBarFrameNotification一样可以监听手机是否横竖屏。 
区别: 当全局禁掉自动旋转屏幕的功能后,手机旋转UIApplicationDidChangeStatusBarFrameNotification不会再发出通知。但UIDeviceOrientationDidChangeNotification会发出通知,无论是否支持横竖屏,都会发出通知。这样可以方便的处理横屏后的页面显示问题。 
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(@"横屏");
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2.两者在监听的方向的多少有区别 
UIApplicationDidChangeStatusBarFrameNotification常见的几种情况:

UIDeviceOrientationPortrait  
UIDeviceOrientationPortraitUpsideDown  
UIDeviceOrientationLandscapeLeft  
UIDeviceOrientationLandscapeRight 
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

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; 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

单页面需要横屏

//在UIApplication实现该方法
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if(self.isFull){

        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait;

}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在需要支持横屏的页面修改isFull的值,并重写方法

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeRotate:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
    AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
    appDelegate.isFull = YES;

 // 支持设备自动旋转
- (BOOL)shouldAutorotate
{
    return YES;
}
// 支持横竖屏显示
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAll;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值