代码之家  ›  专栏  ›  技术社区  ›  cmii

如何从info.plist获取和使用uiInterfaceOrientation项?

  •  0
  • cmii  · 技术社区  · 6 年前

    我想在info.plist中获取支持的接口方向的项0。

    enter image description here

    为了得到这样的东西:

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
         NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
    
         return supportedOrientations[0]; 
    

    }

    但我当然有一个不兼容类型错误: Incompatible pointer to integer conversion returning 'id' from a function with result type

    如何解决这个问题?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Adeel Miraj    6 年前

    正如@mats在他的评论中提到的,信息字典包含支持方向的字符串值。您需要将字符串值转换为期望值 UIInterfaceOrientationMask 键入。可以使用字符串类别来执行此操作。

    nsstring+ui设备定向.h

    static NSString *kUIInterfaceOrientationPortrait           = @"UIInterfaceOrientationPortrait";
    static NSString *kUIInterfaceOrientationLandscapeLeft      = @"UIInterfaceOrientationLandscapeLeft";
    static NSString *kUIInterfaceOrientationLandscapeRight     = @"UIInterfaceOrientationLandscapeRight";
    static NSString *kUIInterfaceOrientationPortraitUpsideDown = @"UIInterfaceOrientationPortraitUpsideDown";
    
    @interface NSString (UIDeviceOrientation)
    
    - (UIInterfaceOrientationMask)deviceOrientation;
    
    @end
    

    nsstring+ui设备定向.m

    @implementation NSString (UIDeviceOrientation)
    
    - (UIInterfaceOrientationMask)deviceOrientation {
        UIInterfaceOrientationMask mask = UIInterfaceOrientationMaskAll;
    
        if ([self isEqualToString:kUIInterfaceOrientationPortrait]) {
            mask = UIInterfaceOrientationMaskPortrait;
        }
        else if ([self isEqualToString:kUIInterfaceOrientationLandscapeLeft]) {
            mask = UIInterfaceOrientationMaskLandscapeLeft;
        }
        else if ([self isEqualToString:kUIInterfaceOrientationLandscapeRight]) {
            mask = UIInterfaceOrientationMaskLandscapeRight;
        }
        else if ([self isEqualToString:kUIInterfaceOrientationPortraitUpsideDown]) {
            mask = UIInterfaceOrientationMaskPortraitUpsideDown;
        }
    
        return mask;
    }
    

    SWIFT版本

    extension String {
    private var kUIInterfaceOrientationPortrait: String {
        return "UIInterfaceOrientationPortrait"
    }
    
    private var kUIInterfaceOrientationLandscapeLeft: String {
        return "UIInterfaceOrientationLandscapeLeft"
    }
    
    private var kUIInterfaceOrientationLandscapeRight: String {
        return "UIInterfaceOrientationLandscapeRight"
    }
    
    private var kUIInterfaceOrientationPortraitUpsideDown: String {
        return "UIInterfaceOrientationPortraitUpsideDown"
    }
    
    public var deviceOrientation: UIInterfaceOrientationMask {
    
        switch self {
        case kUIInterfaceOrientationPortrait:
            return .portrait
    
        case kUIInterfaceOrientationLandscapeLeft:
            return .landscapeLeft
    
        case kUIInterfaceOrientationLandscapeRight:
            return .landscapeRight
    
        case kUIInterfaceOrientationPortraitUpsideDown:
            return .portraitUpsideDown
    
        default:
            return .all
        }
    }
    }