Loading custom UIView in UIViewController's main view

14,727

Solution 1

After a lot of trial and error I found a solution based on an approach explained in the following question, answered by Brian Webster.

The solution was originally suggested for a Cocoa environment. I hope it is valid in an iOS environment as well.

  1. Create the main view controller with a NIB-file. In the NIB, the File's Owner should correspond to the class of your main view controller.
  2. Create a custom view controller with a NIB-file. In this NIB, the File's Owner should correspond to the class of your custom view controller.
  3. Create a custom view controller property in your main view controller class.
  4. Create an UIView property in the main view controller class. It will hold your custom view controller's view. Define it as an IBOutlet, so it can be linked in the NIB.
  5. Drop a UIView in your main view controller's NIB. Link it to the main view controller's view IBOutlet. It will be used as a placeholder for the custom view.
  6. In the main view controller's viewDidLoad method, load the custom view controllers NIB, determine the custom view's frame size and copy the view in the main view controller's view.

Here is some code:

  • MainViewController.h

    @interface MainViewController : UIViewController {
      CustomViewController *customViewController;
      UIView *customView;
    }
    @property (nonatomic, retain) CustomViewController *customViewController;
    @property (nonatomic, retain) IBOutlet UIView *customView;
    @end
  • MainViewController.m

    - (void)viewDidLoad {
      CustomViewController *controller = [[CustomViewController alloc] initWithNibName:@"CustomViewController" bundle:nil];
      self.customViewController = controller;
      [controller release];
      customViewController.view.frame = customView.frame;
      customViewController.view.autoresizingMask = customView.autoresizingMask;
      [customView removeFromSuperview];
      [self.view addSubview:customViewController.view];
      self.customView = customViewController.view;
      [super viewDidLoad];
    }
    

Solution 2

  1. Add an IBOutlet propertyfor your custom UIView to the UIViewController, and additional outlets for any subviews you wish to access.
  2. Go to Interface Builder, select the "File's Owner" object in your NIB and in the Inspector go the rightmost tab set its class to match your UIViewController's class.
  3. Connect the IBOutlet from step one on the "File's Owner" to your custom UIView.
  4. In XCode, when you need to load your view, do something like this:

--

   [[NSBundle mainBundle] loadNibNamed:@"MyNib" owner:self options:0];
   self.myCustomView.frame=self.view.bounds; // make view fill screen - customize as necessary
   [self.view addSubview:self.myCustomView];

When you load the NIB, the outlet(s) you set up in step 1 will be populated with the objects loaded from your NIB.

Share:
14,727
Beav
Author by

Beav

Updated on June 04, 2022

Comments

  • Beav
    Beav almost 2 years

    I have subclassed UIView and created a NIB that controls the main logic for my application.

    Hoping the view will scale nicely, I want to use it for both the iPhone and iPad versions of the app.

    On the iPhone the view will cover the full screen. On the iPad the view will cover only part of the screen.

    I have read that you shouldn't use UIViewControllers to control only part of the screen. So, I am trying to embed the custom UIView in the main UIViewController's view using IB.

    How can this be done?