Alex Rivera | Logout

How to addSubview with a position?

Asked 2010-08-19T04:50:58.290
31

I have something like this:

myViewController = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil];
[mainCanvas addSubview: myViewController.view];
self.view = mainCanvas;

It will be added at the position (0, 0), but I want to add it at (0, 100) or somewhere else. How can I do so?

Edit
Report

1 Answer

7

This is the best way I've found to add a subView, like a loading screen or something that you want to show whether you are in a UIView, UIScrollView, or UITableView.

myViewController = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil]; 
myViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:myViewController.view];
self.view.bounds = myViewController.view.bounds;

This will make the subView appear in full screen no matter where you are in the self.view by adding the subView to where you are currently located in your self.view instead of positioning it in the top left corner, only showing fully if you are at the very top of your view.

answered 2011-05-11T18:15:49.057

Your Answer