CGRect positioning according to center point
Asked Answered
E

4

6

I'm trying to create multiple views for an iPad app. First I created a menu view and then a sub-menu view with coordinates according to menu. So it looks like this:

enter image description here

What I did:

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

But now on sub-menu I'm trying to create the content view, which is a UINavigationController. Trying to do the same thing, I get this result:

enter image description here

What I'm doing (this time creating the frame on sub-menu 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);

It's pretty self-explanatory but, I just get the sub-menu origin and add its width so I can get the right edge coordinate.

After a lot of attempts I managed to get it working, because I noticed that the CGRectMake is using the center of the UINavigationController view to arrange 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);

Yields the right position.

What's going on? I thought CGRectMake origin would always be top-left, but on this particularly view is actually top-middle (or middle-middle, not sure.) What am I doing wrong?

EDIT1:

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

enter image description here

Notice how the nav bar is now positioned right:

enter image description here

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

EDIT2:

I eventually gave up. The problem was with the automatically generated UINavigationBar, which is created by the UINavigationViewController. I can't seem to figure out how to configure it. I'm gonna leave the question open in case someone knows the answer.

Encroachment answered 12/8, 2013 at 14:27 Comment(5)
well it depends in what view you are using. The origin technically is always the top left in a UI element object. If you have lets say another UIView object as your subview, then you are going to have to accommodate the subview positing. It will seem to start in the middle of the self.view, but that is because it will be starting at (0,0) like in the regular self.view Hope this helpsProfound
This may help you : #6785323Etan
#11252488Etan
Well, you have to understand what is the frame, what is view hierarchy (the frame is relative to the top-left corner of the superview) and probably also autoresizing masks. CGRect is only a group of four numbers, nothing else. The important thing is how it is interpreted by the view layout mechanism.Kenosis
@Profound well that navigation bar is created 'automatically', I just alloc'd and initiated a blank view with that frame. Is that why? Should I create a View only to correctly align the nav bar?Encroachment
C
13

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

Comitia answered 12/8, 2013 at 15:32 Comment(4)
You don't have to calculate this manually, there are methods called CGRectGetMidX and CGRectGetMidY defined in CGGeometry.hFanechka
This isn't exactly what I'm asking, I think.Encroachment
But it gives you what you need. @abizern nice. I knew the NS ones. Didn't know the CG equivalent.Comitia
If possible you should consider Auto Layout as it will make this really easy.Comitia
M
5
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 is

Manuscript answered 28/7, 2015 at 0:14 Comment(1)
I think you mean "btn.center" in your second line.Claustrophobia
I
1

CGRectMake just creates an stucture of (x,y, witdh, height).
It your part to set the correct values.

Ineffaceable answered 12/8, 2013 at 14:34 Comment(4)
Could you please elaborate more? I mean I know that, but am I setting the values wrong?Encroachment
first it helps to not inline for debugging. create 4 local variables x,y,w,h and assign values to that, then create the CGRect. 2) your x coord looks strange x + width is outside of the view. the x of the subviews frame must be between frame.x and frame.x + width.Ineffaceable
But shouldn't frame.x > xcoord > frame.x + width give me a frame inside the sub-menu?Encroachment
Edited the question with more infoEncroachment
B
-1

thing.center = CGPoint(x: bounds.midX, y: bounds.midY)

It's that simple.

Boyceboycey answered 17/7 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.