[英]How to get and use UIInterfaceOrientation items from info.plist?
I would like get the item0 of the supported interface orientations in my info.plist. 我想在我的info.plist中获取受支持的界面方向的item0。
In order to have something like this: 为了拥有这样的东西:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
return supportedOrientations[0];
} }
But of course I have an incompatibility type error: Incompatible pointer to integer conversion returning 'id' from a function with result type
但是,当然我有一个不兼容的类型错误: Incompatible pointer to integer conversion returning 'id' from a function with result type
How to solve this? 如何解决呢?
As @Mats mentioned in his comment, the info dictionary contains string values for the supported orientations. 正如@Mats在其评论中提到的那样,信息字典包含受支持方向的字符串值。 You need to convert the string value to the desires UIInterfaceOrientationMask
type. 您需要将字符串值转换为所需的UIInterfaceOrientationMask
类型。 You may use a string category to do this. 您可以使用字符串类别来执行此操作。
NSString+UIDeviceOrientation.h NSString + UIDeviceOrientation.h
static NSString *kUIInterfaceOrientationPortrait = @"UIInterfaceOrientationPortrait";
static NSString *kUIInterfaceOrientationLandscapeLeft = @"UIInterfaceOrientationLandscapeLeft";
static NSString *kUIInterfaceOrientationLandscapeRight = @"UIInterfaceOrientationLandscapeRight";
static NSString *kUIInterfaceOrientationPortraitUpsideDown = @"UIInterfaceOrientationPortraitUpsideDown";
@interface NSString (UIDeviceOrientation)
- (UIInterfaceOrientationMask)deviceOrientation;
@end
NSString+UIDeviceOrientation.m NSString + UIDeviceOrientation.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 version 迅捷版
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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.