Iphone – Setting the size of a UIView

iphoneuiviewuiviewcontroller

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!

Best Answer

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

Related Topic