UIView setCenter Task

I have a lot of trouble doing something very simple. I have a viewController and a subViewController, as with views loaded from nib. For clarification, the parent view is 1024 by 748, and the subView is 640 by 480 in tips.

In the viewControllerDidLoad view, I add a subViewController view as follows:

[self.view addSubview:self.subViewController.view]; NSLog(@"subview.frame:%@",NSStringFromCGRect(self.subViewController.view.frame)); 

Log outputs:

 subview.frame:{{0, 0}, {640, 480}} 

Then, to check everything, I use setCenter to try to move the subview in the parent view in the same position as in the following:

 CGPoint newCenter = CGPointMake(0,0); //should stay at the same position.. [self.subViewController.view setCenter:newCenter]; NSLog(@"after changing center.subview.frame:%@",NSStringFromCGRect(self.subViewController.view.frame)); 

Log outputs:

 after changing center. subview.frame:{{-320, -240}, {640, 480}} 

Now I have a feeling that something can be plunged into the umbilical cord, and I guess that it has something to do with the springs and spacers / sizes and positions. I can only guess about it because this function has the least understanding of when it comes to IB.

Any other ideas as to what might cause this problem? Do you have any general advice on how to configure viewcontrollers in IB to automatically position themselves in accordance with other viewcontrollers? And after setting up, is SetCenter installed the correct way to dynamically move them?

+4
source share
2 answers

The outputs are correct, considering what you are doing. Point (0,0) - upper left corner. If you set a subview for this center, then the upper left corner of the subset will indeed be (-640/2, -480/2) .

To align the upper left corners, use

 [self.subViewController.view setCenter:CGPointMake(320.0,240.0)]; 
+6
source

The problem is unclear. If you set the center value to 0.0, then you should expect the start of the frame to be -320, -240, as it seems. Perhaps you misunderstood CGRect? CGRect consists of source and size. The frame is a CGRect. The frame, size and center are connected to each other, and also change the size and center of the frame, as well as change the frame of the center.

0
source

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


All Articles