views:

917

answers:

2

Hi there,

I have a UIViewController subclass to control a UIView that I want to add to a UIScrollView. I only want the view to be 100px high, but when I add it to the scroll view it gets made 460px high, ignoring the frame size I set:

MyViewController *vc = [[MyViewController alloc] init];
vc.view.frame = CGRectMake(0, 0, 320, 100);
myScrollView.autoresizesSubviews = NO
[myScrollView addSubview:vc.view];
[vc release];

I have set the scroll view to not autoresize subviews but it seems this is still happening! What can I do?

I have also tried setting the frame size inside loadView: in the UIViewController (which is where I will add all my controls and will need access to the size of the view) but that doesnt work either!

- (void)loadView {
    [super loadView];
    self.view.backgroundColor = [UIColor redColor];
    self.view.frame = CGRectMake(0, 0, 320, 100); // still doesnt work
}

Any help would be greatly appreciated!

A: 

You are using loadView incorrectly, im even suprised you see a view (you shouldnt since in load view you arent assigns the vc view to anything), in loadView you must assign your view to a new UIView i nstance, anyway, you should be doing the same but in viewDidLoad instead of load view, that might work for you

Daniel
Yes I'm seeing a view as the call to [super loadView] creates a plain UIView object. I just don't understand why it's autoresizing the view!
Michael Waterfall
try doing it in view did load, im not sure exactly what you are doing
Daniel
Ah, fixed it now! Really not sure how but it's working! I'm using loadView to setup my views and then viewDidLoad to fill my view with the current data and settings. I believe this is fine. Thanks for your help!
Michael Waterfall
A: 

Here is a snippet of how I do it. Note that the origin is with respect to the view you are adding to (in my case 'self').

appRect.origin=CGPointMake(0.0, 0.0);// origin
appRect.size = CGSizeMake(320.0f, 100.0f); //size
CGRect frame = CGRectInset(appRect, 0.0f, 0.0f);
gv=[[GraphicsView alloc] initWithFrame:appRect object:[model me]];
[gv setFrame:frame];

[self.view addSubview:gv];
[gv release];
John Smith