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.

diogocarmo
  • 960
  • 1
  • 11
  • 30
  • 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 helps – user2277872 Aug 12 '13 at 14:31
  • This may help you : http://stackoverflow.com/questions/6784823/draw-an-image-in-the-center-of-another-rectangle-using-canvas – CRDave Aug 12 '13 at 14:31
  • http://stackoverflow.com/questions/11251988/how-to-center-a-subview-of-uiview – CRDave Aug 12 '13 at 14:32
  • 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. – Sulthan Aug 12 '13 at 14:40
  • @user2277872 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? – diogocarmo Aug 12 '13 at 16:13

3 Answers3

13

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

Alexandre Cassagne
  • 2,384
  • 23
  • 40
uchuugaka
  • 12,679
  • 6
  • 37
  • 55
  • 16
    You don't have to calculate this manually, there are methods called `CGRectGetMidX` and `CGRectGetMidY` defined in [CGGeometry.h](https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CGGeometry/Reference/reference.html#//apple_ref/c/func/CGRectGetMidX) – Abizern Aug 12 '13 at 15:46
  • 1
    This isn't exactly what I'm asking, I think. – diogocarmo Aug 12 '13 at 16:15
  • But it gives you what you need. @abizern nice. I knew the NS ones. Didn't know the CG equivalent. – uchuugaka Aug 13 '13 at 00:11
  • 1
    If possible you should consider Auto Layout as it will make this really easy. – uchuugaka Aug 13 '13 at 00:12
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

Youstanzr
  • 605
  • 1
  • 8
  • 16
1

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

AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • Could you please elaborate more? I mean I know that, but am I setting the values wrong? – diogocarmo Aug 12 '13 at 16:14
  • 1
    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. – AlexWien Aug 12 '13 at 16:21
  • But shouldn't frame.x > xcoord > frame.x + width give me a frame inside the sub-menu? – diogocarmo Aug 12 '13 at 16:28
  • Edited the question with more info – diogocarmo Aug 12 '13 at 16:33