views:

887

answers:

3

Hy i'm having problem when using addSubview. Example code:

ParentView *myParentView = [[ParentView alloc] initWithNibName:@"ParentView " bundle:nil];
ChildeView *myChildeView = [[ChildeView alloc] initWithNibName:@"ChildeView" bundle:nil];

//...  parent frame resized with setFrame lets say to x:0, y:0, W:320, H:411

[[myParentView view] addSubview: [myChildeView view]];

My chield when added is bigger then the parent, and do not resize it frame to parent bounds. I can't use "clip subviews" on the parent , and "Autoresize Subviews" seems not to work if parent frame do not resized again. Is there a property that make subviews resize automatically to it's parent bounds, without using setFrame on every child.

A: 

Have you tried setting the child view's autoresize mask? Try this:

[ myChildeView setAutoresizingMask:( UIViewAutoresizingFlexibleWidth |
                                     UIViewAutoresizingFlexibleHeight )];

Also, you may need to call

[ myParentView setAutoresizesSubviews:YES ];

to get the parent view to resize the child view(s) automatically.

Jeff Kelley
Thanks for the answer, although it doesn't work. But why isn't this the default UIView behavior. Am i produceing funny code? And why clip Subviews on the other hand works?
Luca
Are you changing the frame of the parent view before you add the child view?
Jeff Kelley
Yes, always getting the same problem.
Luca
I think you're just going to have to call setFrame when you add the child view. Sorry.
Jeff Kelley
Ok, thanks anyway. If I find another way will add comment.
Luca
This doesn't work. It's a bug in Apple's SDK, I have no idea what causes it, except it seems to be related to the childview being larger than the parent. It *seems* that Apple's got a typo in their source code somewhere, so that they're incorrectly ignoring the add.
Adam
A: 

Yes, I also tried to make parent view a simple UIView controle placed and resized in IB.

Luca
A: 

You can always do it in your UIViews - (void)didMoveToSuperview method. It will get called when added or removed from your parent (nil when removed). At that point in time just set your size to that of your parent. From that point on the autoresize mask should work properly.

Sean
Informs the receiver that its superview has changed (possibly to nil). It seems that it doesn't fire at addSubview.
Luca