advantis
6/5/2013 - 11:43 AM

Fix for UIStoryboard unable to recognize device-specific resource names

Fix for UIStoryboard unable to recognize device-specific resource names

//
//  Copyright © 2013 Yuri Kotov
//

#import <objc/runtime.h>

NS_INLINE NSString * DeviceSpecificSuffix(UIUserInterfaceIdiom idiom)
{
    switch (idiom)
    {
        case UIUserInterfaceIdiomPhone:
            return @"~iphone";
        case UIUserInterfaceIdiomPad:
            return @"~ipad";
        default:
            return @"";
    }
}

// HACK: Fix for rdar://12753707
@implementation UIStoryboard (ADVDeviceModifierSupport)

+ (void) load
{
    @autoreleasepool
    {
        method_exchangeImplementations(class_getClassMethod(self, @selector(storyboardWithName:bundle:)),
                                       class_getClassMethod(self, @selector(ADV_storyboardNamed:bundle:)));
    }
}

+ (UIStoryboard *) ADV_storyboardNamed:(NSString *)name bundle:(NSBundle *)bundleOrNil
{
    UIUserInterfaceIdiom idiom = [[UIDevice currentDevice] userInterfaceIdiom];
    NSString *deviceSpecificName = [name stringByAppendingString:DeviceSpecificSuffix(idiom)];

    NSBundle *bundle = bundleOrNil ?: [NSBundle mainBundle];
    if ([bundle pathForResource:deviceSpecificName ofType:@"storyboardc"])
    {
        name = deviceSpecificName;
    }
    
    return [self ADV_storyboardNamed:name bundle:bundleOrNil];
}

@end