Can I use two xibs with one viewcontroller - re: porting to iPhone 5

后端 未结 3 1188
无人共我
无人共我 2020-12-23 23:03

I just submitted my first app to the app store (yay it was just approved!). I now want to update it to work with (look nicer on) the larger iPhone 5 screen. I don\'t intend

3条回答
  •  感动是毒
    2020-12-23 23:31

    Code in answer was helpful, but I needed something that worked better for universal apps (iphone/ipad).

    In case someone else needs the same thing, here's something to get you started.

    Say you built a universal app using the nib/xib naming standards for ios for view controllers that have xibs with the same name:

    The two built-in defaults for autoloading xibs when providing no name is passed to initWithNibName:

    • ExampleViewController.xib [iphone default when nib named empty for Retina 3.5 Full Screen for classic layouts iphone 4/4s etc...]
    • ExampleViewController~ipad.xib [ipad/ipad mini default when nib named empty]

    Now say you need custom xibs for the iphone 5/5s in IB using Retina 4 Full Screen option, i.e., you don't want the 3.5 xibs displaying for any 568h devices.

    Here's the custom naming convention using a category approach:

    • ExampleViewController-568h.xib [iphone non default/custom naming convention when nib name empty for Retina 4 Full Screen (568h)]

    Instead of overriding the built-in naming defaults, use a category to help set the right xib for the controller.

    https://gist.github.com/scottvrosenthal/4945884

    ExampleViewController.m

    #import "UIViewController+AppCategories.h"
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        nibNameOrNil = [UIViewController nibNamedForDevice:@"ExampleViewController"];
    
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
    
           // Do any additional customization 
    
        }
    
        return self;
    }
    

    UIViewController+AppCategories.h

    #import 
    
    @interface UIViewController (AppCategories)
    
       + (NSString*)nibNamedForDevice:(NSString*)name;
    
    @end
    

    UIViewController+AppCategories.m

    // ExampleViewController.xib [iphone default when nib named empty for Retina 3.5 Full Screen]
    // ExampleViewController-568h.xib [iphone custom naming convention when nib named empty for Retina 4 Full Screen (568h)]
    // ExampleViewController~ipad.xib [ipad/ipad mini default when nib named empty]
    
    #import "UIViewController+AppCategories.h"
    
    @implementation UIViewController (AppCategories)
    
    + (NSString*)nibNamedForDevice:(NSString*)name
    {
    
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {
            if ([UIScreen mainScreen].bounds.size.height == 568)
            {
                //Check if there's a path extension or not
                if (name.pathExtension.length) {
                    name = [name stringByReplacingOccurrencesOfString: [NSString stringWithFormat:@".%@", name.pathExtension] withString: [NSString stringWithFormat:@"-568h.%@", name.pathExtension ]
                    ];
    
                } else {
                    name = [name stringByAppendingString:@"-568h"];
                }
    
                // if 568h nib is found
                NSString *nibExists = [[NSBundle mainBundle] pathForResource:name ofType:@"nib"];
                if (nibExists) {
                    return name;
               }
    
            }
        }
    
        // just default to ios universal app naming convention for xibs
        return Nil;
    }
    
    @end
    

提交回复
热议问题