iOS横竖屏问题

 最近突然被告知要适配横屏,当然最麻烦的是你还不知道iOS工程到底允不允许横屏,或许说的有点绕口,待会儿看会细说的。其实很想感慨一句:各种奇葩的合理不合理的需求都会遇到的,做为程序员能做的只有打好基础才能应对各种情况。
 先说几个API吧,获取iOS项目工程自带的plist文件:
NSDictionary *plist = [[NSBundle mainBundle] infoDictionary];
然后你可以在这里获得你的项目配置,有兴趣的可以自己看看这个字典里有哪些key,跟今天我要说的有关的是这个:UISupportedInterfaceOrientations
NSArray *arryplist = plist[@"UISupportedInterfaceOrientations"];
你获取的这个arryplist里面的元素是你在项目配置中设置的允许这个app支持哪些方向是直立的、向左横屏、向右横屏、颠倒。最多会有这四个元素(因为工程配置中最多只支持这个四个方向的):UIInterfaceOrientationPortrait、UIInterfaceOrientationPortraitUpsideDown、UIInterfaceOrientationLandscapeLeft、UIInterfaceOrientationLandscapeRight。
 通过以上两行代码你可以获取到这个APP支持哪些方向的。


 还要在说个小技巧,你可以通过监听这个通知实时获取到屏幕方向改变的消息:UIDeviceOrientationDidChangeNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeFrame) name:UIDeviceOrientationDidChangeNotification object:nil];
不要忘了还要移除通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}

这样当手机屏幕方向改变时候你就可以知道了。
今天最后再说个API通过这个方法你可以获取到当前手机是横屏、还是竖屏、其他位置状态:

[UIDevice currentDevice].orientation

你获取到的值是个枚举类型,有这些值:

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    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
} __TVOS_PROHIBITED;

配合以上三个技巧,你可以进行横竖屏的适配:

- (void)changeFrame {
    CGRect rect = [UIScreen mainScreen].bounds;
    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height;
    NSDictionary *plist = [[NSBundle mainBundle] infoDictionary];
    NSArray *arryplist = plist[@"UISupportedInterfaceOrientations"];

    switch ([UIDevice currentDevice].orientation) {
        case UIDeviceOrientationPortrait://这是竖屏
            for (NSString *stringplist in arryplist) {
                if ([stringplist isEqualToString:@"UIInterfaceOrientationPortrait"]) { //判断这个APP是否允许竖屏,允许了再改变frame

                }
            }

            break;
        case UIDeviceOrientationLandscapeLeft:
            for (NSString *stringplist in arryplist) {
                if ([stringplist isEqualToString:@"UIInterfaceOrientationLandscapeLeft"]) {

                    }

                }
            }

            break;
        case UIDeviceOrientationLandscapeRight:
            for (NSString *stringplist in arryplist) {
                if ([stringplist isEqualToString:@"UIInterfaceOrientationLandscapeRight"]) {

                }
            }
            break;
        default:
            break;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值