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

后端 未结 3 1094
无人共我
无人共我 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 <UIKit/UIKit.h>
    
    @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
    
    0 讨论(0)
  • 2020-12-23 23:35

    [Revised with Complete Answer on : Oct 7, 2012]

    After significant research I found the answer, partly on this SO page (which shows how to detect which iPhone version your app is running on) and partly this SO page (showing how to have two xib's share the same 'File's Owner'. The final piece of the puzzle (loading separate xib's via the loadNibNamed: method) I found in chapter 10 of The Big Nerd Ranch's excellent iOS Programming text. Here's how:

    1. Create a second xib (File, New..., File, select 'User Interface', select 'Empty' and save it. This creates the new xib. In the example below, my classic xib (for 3.5" iPhones) was named TipMainViewController.xib. I saved the new iPhone 5 xib with the name 'TipMainViewController-5.xib'

    2. Make that new xib's 'File's Owner' the same ViewController as your existing xib. To do this, in the new xib file, select 'File's Owners'. Then in the 'Identity Inspector' select the existing View Controller as the Custom Class. In my case I selected 'TipMainViewController'.

    3. Drag a new UIView onto the new xib's empty canvas. In the new UIView's attribute inspector set the 'Size' attribute to 'Retina 4 Full Screen'

    4. Select all the contents in the existing 'Classic' 3.5" xib - eg: all your controls, buttons, selectors, labels etc. Copy them and paste them into the new iPhone 5 xib. Resize/move etc. them to optimize for the iPhone's 4" display.

    5. Make all the connections to/from File's Owner as you did when you created your original xib.

    6. Finally, in the 'viewDidLoad' method of your 'single' ViewController, insert the following logic (using your nib/xib names of course):

      - (void)loadView
      {
          [super viewDidLoad];
          if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
          {
              CGSize result = [[UIScreen mainScreen] bounds].size;
              if(result.height == 480)
              {
                // iPhone Classic
                [[NSBundle mainBundle] loadNibNamed:@"TipMainViewController" owner:self options:nil];
              }
              if(result.height == 568)
              {
                // iPhone 5
                [[NSBundle mainBundle] loadNibNamed:@"TipMainViewController-5" owner:self options:nil];
              }
          }
      }
      
    0 讨论(0)
  • 2020-12-23 23:39

    Here is a simple, working code sample for your view controller that shows how to load myXib-5.xib on the iPhone 5 and myXib.xib on iPhones/iPods predating the iPhone 5:

    - (void)loadView
    {
        if([[UIScreen mainScreen] bounds].size.height == 568)
        {
            // iPhone 5
            self.view = [[NSBundle mainBundle] loadNibNamed:@"myXib-5" owner:self options:nil][0];
        }
        else
        {
            self.view = [[NSBundle mainBundle] loadNibNamed:@"myXib" owner:self options:nil][0];
        }
    }
    

    It assumes that you are only targeting the iPhone and not the iPad, to keep it simple.

    The XIB's file owner's class property should also be set to the view controller that contains loadView.

    0 讨论(0)
提交回复
热议问题