CGRect positioning according to the center point

I am trying to create several views for an iPad application. First I created a menu view, and then a submenu with coordinates according to the menu. So it looks like this:

enter image description here

What I've done:

self.view = [[UIView alloc] initWithFrame:CGRectMake(self.parentViewController.view.frame.size.width, 0, 150, screenHeight)]; 

But now in the submenu, I'm trying to create a content view, which is a UINavigationController. Trying to do the same, I get this result:

enter image description here

What am I doing (this time creating a frame on a submenu view controller):

 CGRect frame = CGRectMake(self.view.frame.origin.x + self.view.frame.size.width, 0, [[UIScreen mainScreen] bounds].size.width - self.view.frame.origin.x - self.view.frame.size.width, [[UIScreen mainScreen] bounds].size.height); 

This is pretty clear, but I just get the start of the submenu and add its width to get the coordinate of the right edge.

After many attempts, I managed to get it to work because I noticed that CGRectMake uses the UINavigationController view center to streamline its position. So the following code:

 CGRect frame = CGRectMake(self.view.frame.origin.x + self.view.frame.size.width + 259, 0, [[UIScreen mainScreen] bounds].size.width - self.view.frame.origin.x - self.view.frame.size.width, [[UIScreen mainScreen] bounds].size.height); 

Sets the correct position.

What's happening? I thought that the origin of CGRectMake will always be top left, but this is actually a top-midpoint view (or mid-midpoint, not sure). What am I doing wrong?

EDIT1:

Here's the console output for the frame with the correct position:

enter image description here

Note that the navigation bar is now positioned correctly:

enter image description here

But the x-coordinate is not 250 (as it should be, because menu.width + sub-menu.width = 250.)

EDIT2:

In the end, I gave up. The problem was the automatically generated UINavigationBar, which is created by the UINavigationViewController. I can’t figure out how to configure it. I am going to leave the question open if someone knows the answer.

+4
source share
3 answers

Center a CGRect is a CGPoint created from origin.x + ( size.width / 2 ) and origin.y + ( size.height / 2 ) .

+8
source
 UIButton* btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 70)]; btn.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2); 

This will make the button in the center no matter what the device

+4
source

CGRectMake just creates a structure (x, y, witdh, height).
It is your role to set the correct values.

+1
source

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


All Articles