views:

2371

answers:

2

I have a ViewController which controls many subviews. When I click one of the buttons I initialize another viewcontroller and show it's view as the subview of this view. However the subview exceeds the bounds of the frame for subview and infact fills the entire screen.

What could be wrong? I presume the problem is that UIViewController's view has a frame (0,0,320,460) and hence fills the entire screen (though it receive's touch events only when touched within the subview frame bounds). How can I resize the frame to fit as subview.

In short, I need help adding a viewcontroller's view as a subview to another viewcontroller's view.

Thanks!

A: 

change the frame size of viewcontroller.view.frame and add to subview [viewcontrollerparent.view addSubview:viewcontroller.view]

CiNN
I tried it. It doesn't work. Should I do something in the interface builder.
sperumal
+2  A: 

Don't do this! The intent of the UIViewController is to drive the entire screen. It just isn't appropriate for this, and it doesn't really add anything you need.

All you need is an object that owns your custom view. Just use a subclass of UIView itself, so it can be added to your window hierarchy and the memory management is fully automatic.

Point the subview NIB's owner a custom subclass of UIView. Add a contentView outlet to this custom subclass, and point it at the view within the nib. In the custom subclass do something like this:

- (id)initWithFrame: (CGRect)inFrame;
{
    if ( (self = [super initWithFrame: inFrame]) ) {
     [[NSBundle mainBundle] loadNibNamed: @"NibNameHere"
                                   owner: self
                                 options: nil];
     contentView.size = inFrame.size;
     // do extra loading here
     [self addSubview: contentView];
    }
    return self;
}

- (void)dealloc;
{
    self.contentView = nil;
    // additional release here
    [super dealloc];
}

(I'm assuming here you're using initWithFrame: to construct the subview.)

Steven Fisher