tags:

views:

305

answers:

3

Hello All,

I want to add a sub view in current view, this sub view is 300x300. When I add subview using

[self.view addSubview:md.view];

the md.view will appear at position (0,0) is there any way to add subview in center?

Thanks

+1  A: 

You can set view's center property:

md.view.center = self.view.center;

Or you can explicetly set frame for md.view so that it will be centered as you want.

Vladimir
Thanks worked like charm!!!
Saurabh
+1  A: 

Use

CGRect bounds = self.view.bounds;
md.view.center = CGPointMake(bounds.width / 2, bounds.height / 2);

before or after that -addSubview: line.

KennyTM
Thanks for your response!
Saurabh
+1  A: 

You can specify exactly where you want the sub view to be placed within the parent view by doing so in the viewDidLoad method as follows:

- (void)viewDidLoad {
   [super viewDidLoad];
   SubView1Controller *subView1Controller=[[[SubView1Controller alloc] initWithNibName:@"SubView1" bundle:nil] autorelease];
   CGRect r = [subView1Controller.view frame];
   r.origin.x = 50;
   r.origin.y = 50;
   [subView1Controller.view setFrame:r];
   [self.view insertSubview:subView1Controller.view atIndex:0]; 
}