views:

32

answers:

1

Hello,

(For example) I would like to have my background image – a UIImageView – always the same size as the UIViewControllers view.

I am unable to figure this out, but sure it is possible. I'm making some UI Elements which I would like to reuse, and which can layout dynamically depending on how big my view controller's view is. How can this be done?

Is it possible not to have to set the frame of the subviews?

Here is some code for an example, in my UIViewController:

- (void)viewDidLoad {
     [super viewDidLoad];

     UIImage *backgroundImage = [[UIImage imageNamed:@"tabBarBackground.png"] stretchableImageWithLeftCapWidth:10.0f topCapHeight:10.0f];
     UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];

    [self.view addSubview:backgroundImageView];
    [backgroundImageView release];
}

II know I could set the frame to match the UIViewControllers frame, but what happens if this layout later need changed? Should I implement my own setNeedDisplay method in this view controller? Or can it some how be done through, maybe, autoresizingMask or other?

I would like to be able to change the view controller view's size, and then the view controller can then rearrange everything as nessicary? Is this the correct approach?

Thanks Ross

+2  A: 

At first, you should initiate the view to the size you want it to be based on the parent view. Then you can run the following, which would keep the subview the same size as its parent as its parent is resized:

view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
rickharrison