Add to center view - iPad

I want to add an additional view in the current view, this additional view is 300x300. When I add subview using

[self.view addSubview:md.view]; 

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

thanks

+4
source share
3 answers

You can set the center view:

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

Or you can explicitly set the frame for md.view so that it is centered as you wish.

+15
source

Using

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

before or after this line -addSubview:

+3
source

You can specify exactly where you want the view to be placed in the parent view by doing this 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]; } 
+2
source

Source: https://habr.com/ru/post/1312181/


All Articles