tags:

views:

1154

answers:

2

I've been looking for background information on resizing, but I haven't been able to find much. I know I need to set autoresizesSubviews on the superview and autoresizingMask on the subview.

I have done this, and my UIImageViews properly resize, but my custom UIView subclass does not.

The code for the UIImageView:

UIImageView *newX = [[[UIImageView alloc] initWithImage:dot] autorelease];
[newX setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[newX setFrame:CGRectMake(0, 0, self.view.bounds.size.width, 1)];
[self.view addSubview:newX];

The code for the custom UIView subclass:

trace = [[TraceView alloc] initWithFrame:self.view.bounds];
[trace setAutoresizingMask:(UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin)];
[self.view addSubview:trace];

From what I can tell, the view rotates as expected, but does not resize. I can no longer see the bottom of it, and it doesn't fill the screen to the right. Do I have to add anything to the UIView subclass to make resizing work properly?

EDIT: I've changed the autoresizingMake to (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight) and called [subview setNeedsDisplay]; I'm not sure why this is necessary as the UIImageViews work fine without it, but it now behaves as expected.

+2  A: 

I think you're missing UIViewAutoresizingFlexibleHeight or width on the subview. Otherwise I think the view just moves around according to the margins but doesn't resize.

Nimrod
Apple's documentation says UIViewAutoresizingFlexibleBottomMargin causes "The view resizes by expanding or shrinking in the direction of the left margin." Which is what I want. I don't want the view to stretch. Further, even if I do use FlexibleWidth/Height, it doesn't do what one would expect. Although it does resize, as soon as I call `setNeedsDisplayInRect:` on the subview it undoes the resizing, and from then on, the resizing is incorrect. It repeats the resizing/unresizing, but using this new shape that doesn't fill the superview as it should.
David Kanarek
The apple docs lie then. You don't get any stretching without the FlexibleHeight/Width options.BTW, there's some sample code in an archive called "Beginning iPhone Development Projects" that you can get from http://iphonedevbook.com/forum/viewforum.php?f=54 that you can build and mess with to get a better understanding of how all this autoresizing stuff works. The projects are called "05 Autoresize 1" and "05 Autoresize 2". I find the resizing model a bit confusing and don't really get it without just playing with the options.
Nimrod
Thanks for the pointer, I've messed around with it a bit and I'm starting to get the hang of it, but the IB and programatic ways of doing this don't seem to map to each other very nicely. Guess it's time for another question.
David Kanarek
A: 

You mention rotation. Is the superview that contains your custom view actually resizing correctly? If in doubt, set its background to some odd color. If it did not resize correctly after the orientation change then you should see a white or grey bar on the right.

St3fan
Yup, it rotates and resizes just fine.
David Kanarek