swift:解决iOS8中appearanceWhenContainedInInstancesOfClasses不能用

swift中此方法在iOS9开始才能用,为了解决这个问题需要用oc写个分类,但是考虑到NSObject的子类比如UITabBarItem,所以写基于NSObject的分类
@available(iOS 9.0, *)
public static func appearanceWhenContainedInInstancesOfClasses(containerTypes: [AnyObject.Type]) -> Self
使用的范例--获取UITabBarItem
  if #available(iOS 9.0, *) {
        item = UITabBarItem.appearanceWhenContainedInInstancesOfClasses([LETabBarController.self])
    } else {
        // Fallback on earlier versions
        item = UITabBarItem.my_appearanceWhenContainedIn(LETabBarController.self)
    }

分类文件
#import
#import

@interface NSObject (UIAppearance_Swift) 
+ (instancetype)my_appearanceWhenContainedIn:(Class)containerClass;
//强制旋转屏幕
+ (void)orientationToPortrait:(UIInterfaceOrientation)orientation;
@end

#import "NSObject+UIAppearance_Swift.h"

@implementation NSObject (UIAppearance_Swift)
+ (instancetype)my_appearanceWhenContainedIn:(Class)containerClass {
return [self appearanceWhenContainedIn:containerClass, nil];
}

//强制旋转屏幕
+ (void)orientationToPortrait:(UIInterfaceOrientation)orientation
{
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = orientation;
[invocation setArgument:&val atIndex:2];//前两个参数已被target和selector占用
[invocation invoke];
}

@end

你可能感兴趣的:(swift:解决iOS8中appearanceWhenContainedInInstancesOfClasses不能用)