How to center subview UIView

I have a UIView inside UIView m, and I want the internal UIView always be centered inside the external, without having to resize and height.

I set the spacers and springs so that it is top / left / right / bottom without resizing. But he is still not focused. Any idea?

+118
ios objective-c iphone ipad
Jun 28 '12 at 20:00
source share
13 answers

Objective-c

 yourSubView.center = CGPointMake(yourView.frame.size.width / 2, yourView.frame.size.height / 2); 

swift

 yourSubView.center = CGPoint(x: yourView.frame.size.width / 2, y: yourView.frame.size.height / 2) 
+192
Jun 28 2018-12-12T00:
source share

You can do this and it will always work:

 child.center = [parent convertPoint:parent.center fromView:parent.superview]; 

And for Swift:

 child.center = parent.convert(parent.center, from:parent.superview) 
+274
Jun 28 2018-12-12T00:
source share

Before we get started, recall that the starting point is the top left corner of the CGPoint view. An important thing for understanding the views and parents.

Let's look at this simple code, a view controller, which adds a black square to it:

 class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. createDummyView() super.view.backgroundColor = UIColor.cyanColor(); } func createDummyView(){ var subView = UIView(frame: CGRect(x: 15, y: 50, width: 50 , height: 50)); super.view.addSubview(subView); view.backgroundColor = UIColor.blackColor() } } 

This will create this view: the beginning and center of the black rectangle correspond to the same coordinates as its parent

enter image description here

Now try adding subView another SubSubView and give subSubview the same origin as subView, but make subSubView a child of the subView

We will add this code:

 var subSubView = UIView(); subSubView.frame.origin = subView.frame.origin; subSubView.frame.size = CGSizeMake(20, 20); subSubView.backgroundColor = UIColor.purpleColor() subView.addSubview(subSubView) 

And this is the result:

enter image description here

Because of this line:

 subSubView.frame.origin = subView.frame.origin; 

You expect the purple source of the rectangle to be the same as its parent (black rectangle), but it goes under it, and why? Since when you add a view to another view, the subView "world" frame is now the parent BOUND RECTANGLE, if you have an idea that the origin on the main screen is at the coordinates (15.15) for all its submenus, the top left the angle will be (0,0)

This is why you need to always refer to the parent using the bound rectangle, which is the "world" of its sub-objects, to fix this line:

 subSubView.frame.origin = subView.bounds.origin; 

And, having seen the magic, subSubview is now exactly in it with parental origin:

enter image description here

So, you like "okay, I just wanted to focus my opinion on my parents' opinion, what is it?" well, it doesn’t really matter, you just need to “translate” the parent point of the center, which is taken from it to the center of the parent boundaries thereby:

 subSubView.center = subView.convertPoint(subView.center, fromView: subSubView); 

In fact, you are telling him: “Take your parents to the viewing center and transform it into the world of subSubView.”

And you get this result:

enter image description here

+37
Jul 22 '15 at 14:29
source share

I would use:

 self.childView.center = CGPointMake(CGRectGetMidX(self.parentView.bounds), CGRectGetMidY(self.parentView.bounds)); 

I like to use the CGRect options ...

SWIFT 3:

 self.childView.center = CGPoint(x: self.parentView.bounds.midX, y: self.parentView.bounds.midY); 
+26
Jan 18 '15 at 19:41
source share

1. If you enabled autorun:

  • Tip. To center the view on another startup view, you can use the same code for any two views that share at least one parent view.

First off, disable child views that automate

 UIView *view1, *view2; [childview setTranslatesAutoresizingMaskIntoConstraints:NO]; 
  • If you are UIView + Autolayout or Purelayout :

     [view1 autoAlignAxis:ALAxisHorizontal toSameAxisOfView:view2]; [view1 autoAlignAxis:ALAxisVertical toSameAxisOfView:view2]; 
  • If you use only UIKit-level auto-detection methods:

     [view1 addConstraints:({ @[ [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeCenterX multiplier:1.f constant:0.f], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeCenterY multiplier:1.f constant:0.f] ]; })]; 

2. Without auto power off:

I prefer:

 UIView *parentView, *childView; [childView setFrame:({ CGRect frame = childView.frame; frame.origin.x = (parentView.frame.size.width - frame.size.width) / 2.0; frame.origin.y = (parentView.frame.size.height - frame.size.height) / 2.0; CGRectIntegral(frame); })]; 
+19
Jun 28 2018-12-12T00:
source share

The easiest way:

 child.center = parent.center 
+12
Feb 17 '16 at 16:37
source share

enter image description here

Set this autoresist mask to your internal view.

+8
Apr 14 '14 at 11:43
source share

With IOS9, you can use the layout binding API.

The code will look like this:

 childview.centerXAnchor.constraintEqualToAnchor(parentView.centerXAnchor).active = true childview.centerYAnchor.constraintEqualToAnchor(parentView.centerYAnchor).active = true 

The advantage of this over CGPointMake or CGRect is that with these methods you set the center of the view to a constant, but with this technique you establish the relationship between the two views that will be held forever, no way the parentview changes.

Just make sure before doing this:

  self.view.addSubview(parentView) self.view.addSubView(chidview) 

and set translatesAutoresizingMaskIntoConstraints for each view to false.

This will prevent AutoLayout clutter and crashes.

+8
Jun 24 '16 at 1:17
source share

you can use

 yourView.center = CGPointMake(CGRectGetMidX(superview.bounds), CGRectGetMidY(superview.bounds)) 

And in Swift 3.0

 yourView.center = CGPoint(x: superview.bounds.midX, y: superview.bounds.midY) 
+7
Sep 15 '17 at 8:52
source share

Using the same center in a view and a view is the easiest way to do this. You can do something like this,

 UIView *innerView = ....; innerView.view.center = self.view.center; [self.view addSubView:innerView]; 
+5
Jul 17 '13 at 22:38
source share

Another solution with PureLayout using autoCenterInSuperview .

 // ... UIView *innerView = [UIView newAutoLayoutView]; innerView.backgroundColor = [UIColor greenColor]; [innerView autoSetDimensionsToSize:CGSizeMake(100, 30)]; [outerview addSubview:innerView]; [innerView autoCenterInSuperview]; 

Here's what it looks like:

Center with PureLayout

+3
May 15, '15 at 17:01
source share

I would use:

 child.center = CGPointMake(parent.bounds.height / 2, parent.bounds.width / 2) 

It is simple, short and sweet. If you use @Hejazi's answer above and parent.center set to anything other than (0,0) , your view will not be centered!

+1
Jul 23. '14 at 23:34
source share

In C # or Xamarin.ios we can use like this

imageView.Center = new CGPoint (tempView.Frame.Size.Width / 2, tempView.Frame.Size.Height / 2);

+1
Jun 07 '19 at 4:19
source share



All Articles