Understanding how subview.center = view.center works and why getter and setter do different things

I see a lot of code that explains how to center a subview inside a view. Code examples usually look like this:

SubView.center = view.center;

      

Can anyone explain to me how this works? I just do not understand.

view.center

indicates the center point of the view. For example width 100, height 100, it will return (50.50). I understand.

The setup subview.center

is strange to me. subview.center

will return the center point of the subview. Anyway, by setting it to (50,50), the position of the subview inside it is parent at coordinates 50/50. But after that, access to this property will return, let's say (25,25), if the subtasks themselves were 50 wide and 50 high.

Don't know what I mean? The concept here is strange to me, since the setter and getter serve different functions.

If anyone can explain this, please. Or, if I leave the base, I would like to know that too. I am new to iOS dev.

If I am correct and this is indeed how it works, would you consider this an anti-pattern. Of course, internally .NET something like this would be an anti-pattern. Maybe not for Obj-C?

+3


source to share


4 answers


Therefore, by installing it, it installs it inside the supervisor. When you get the center of the subviews, it gives you the actual center of the view.

So half of 50 is 25, hence 25.25. You want the center of the subview not to be its parent center, so there is no reason for it to return the coordinates of the parent center, just its own.



To be a little more technical, it has to be related to the Frame and Bounds of the view (the receiver gets the center using the border view and the setter uses the view's view). Here is a link that describes what they are and how they differ.

+2


source


To center the subview inside the view. I think the 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 ) );

      

+7


source


The center of the view.center gives the center point of the view in the coordinate space of the view viewiews. Thus, it allows you to move the view to its supervisor.

If you have a size like {100,100} and set its center to {200,200} - its center point will be located {200,200} from the start of its viewing. In fact, it will be inserted at 150 points from the supervisor source.

This way 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 would be {150,150}).

This allows you to correct the size and position of a view object either by using its frame property or by setting its center and border properties.

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

+1


source


From ref UIView class: "Center is specified in its supervisor's coordinate system." So in your example, if you set the center to (50,50) in supervisor mode (via the setter method), the subview will be centered there. And if you read it back (via the getter) it will be (50.50) as well.

0


source







All Articles