4

I am seeing a lot of code that explains how to centre a subview inside a view. The code examples typically go like this:

SubView.center = view.center;

Could someone explain to me how this works? I just don't get it.

The view.center gives the center point of the view. For example width is 100, height is 100, it will return (50,50). I get it.

Setting subview.center is weird to me. subview.center will return the center point of the subview. Somehow, setting it to (50,50) will position the subview within it's parent to coordinates of 50/50. But then accessing this property will return let's say (25,25) if the subview itself was width of 50 and height of 50.

Undestand what I mean? The concept here is weird to me as the setter and getter are doing different functions.

If someone could explain this please do. Or, if I am way off base I would like to know that too. I am new to iOS dev.

If I am correct and this is really the way it works, would you not consider this an anti-pattern. Certainly within .NET something like this would be an anti-pattern. Maybe not for Obj-C?

lucian.pantelimon
  • 3,673
  • 4
  • 29
  • 46
user1060500
  • 1,505
  • 1
  • 21
  • 40

4 Answers4

7

To centre a subview inside a view. I think following code is correct.

Swift 3.0:

SubView.center = CGPoint(x: view.bounds.midX, y: view.bounds.midY );

Swift 2.2:

SubView.center = CGPointMake( CGRectGetMidX( view.bounds ), CGRectGetMidY( view.bounds ) );
buczek
  • 2,011
  • 7
  • 29
  • 40
Satachito
  • 5,838
  • 36
  • 43
2

So when setting it, it is setting it inside it's superview. When getting the subviews center it gives you the actual views center.

So half of 50 is 25, hence 25,25. You are wanting the subview's center not its parent's center so there is no reason for it to return its parent's center coordinates, just its own.

To be a bit more technical it has to related to Frame and Bounds of a view (the getter is getting the center by using the view's bounds and the setter is using the view's frame). Here is a link that describes what those are and how they are different.

Community
  • 1
  • 1
Firo
  • 15,448
  • 3
  • 54
  • 74
1

The view.center gives the center point of the view in the view's superviews coordinate space. So it allows you to reposition a view within it's superview.

If you have a view size {100,100} and set it's center {200,200} - it's centerpoint will be positioned {200,200} from the origin of it's superview. Effectively it will be inset 150 points from the superview's origin.

Thus the view.center and view.bounds.width are not related to each other. But there is a relationship between view.center and view.frame.origin (which in the example will be {150,150}).

This allows you to fix the size and position of a view object either by using it's frame property or by setting it's center and bounds properties.

The center that you describe is not a property of the view, but can be got and set via {view.bounds.size.width/2,view.bounds.size.height/2}.

foundry
  • 31,615
  • 9
  • 90
  • 125
0

From the class ref of UIView: "The center is specified within the coordinate system of its superview". So, in your example, if you set the center to (50,50) in the superview (via the setter method), the subview will be centered there. And if you read it back (via the getter), it will also be (50,50).

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116