views:

56

answers:

2

Hi, When I am switching between Portrait to Landscape view (&Vice Versa) in iPad, position of my popover view gets garbled. Here is how I am calculating frame of my popover view:

aRect = self.myElement.frame;
aRect.origin.x += aRect.size.width;

[aPopOver presentPopoverFromRect:aRect inView:self.myElement.superview permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];

Please tell me whats wrong here?

A: 

ok, i notice something weird about your code.

any reason you are adding the size of the wide to the origin of aRect's x position? aRect.origin.x += aRect.size.width;

im assuming you want this to be the top right corner....

You can uncomment the code in your .m file and make it like so:

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     Return YES; // for supported orientations
    //otherwise return (interfaceOrientation == UIInterfaceOrientationLandscape); if you want only landscape mode.
}

Or what i would do in your situation if you want to layout your subviews is use the didRotateFromIntferfaceOrientation: like so:

(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    [self layoutSubviews];
}

and also layoutSubviews

- (void)layoutSubviews
{
    NSLog(@"layoutSubviews called");

    ...recalc rects etc based on the new self.view.bounds...
}

It works like so.

PK

Pavan
Yes, I am trying this to align on right side in the middle of pop over view. By adding the width in the origin, I am getting it to be placed at correct position when I launch my app. Issue comes when I change the orientation.
Abhinav
so what you should try is the didRotateFromInterfaceOrientation i will update my post in how you can do so.
Pavan
just a question do you have a tab bar in your application?
Pavan
Ok. But what code I should write in 'didRotateFromInterfaceOrientation' method. My pop over display logic is tightly coupled with controller methods and also, my view is made up of custom UI widgets. On one such UI widget I am showing this pop over view.
Abhinav
@abhinav in your didRotateFromInterfaceOrientation you simply write your [self layoutSubviews]; which is a method called which you should create which will simply have code that updates only your GUI. thats all. And in your layoutSubviews method which again simply will contain code that will update the graphical user interface that's that. so you will have stuff like button.x = here andaRect.origin.x += wherever; and reposition just the display so it is fit for landscape mode. thats that. display logic filled with controller methods is irrelevant. you just need to update GUI.
Pavan
first do a test. setup your didRotateFromIntergaceOrientation method as i have mentioned in the post which will contain a one line code that will call the layoutSubviews function with [self layoutSubviews]; , then create a method/function called layoutSubviews so that we can call it. make sure you write no code in that method. Now in this method you will write only NSLog(@"Layout Subviews called"); to makre sure that when running the program didRotateFromInterfaceOrientation actually does get called before you start populating the layoutSubviews with your update of the GUI of your app.
Pavan
Ok Pawan. I am working on it and will get back in case of issues. Thanks so much.
Abhinav
Thats great to hear. Obviously this made sense to you. Thats great. You have enough to work with anyway. Good luck and enjoy.
Pavan
A: 

From the UIPopoverController documentation: (emphasis mine)

If the user rotates the device while a popover is visible, the popover controller hides the popover and then shows it again at the end of the rotation. The popover controller attempts to position the popover appropriately for you but you may have to present it again or hide it altogether in some cases. For example, when displayed from a bar button item, the popover controller automatically adjusts the position (and potentially the size) of the popover to account for changes to the position of the bar button item. However, if you remove the bar button item during the rotation, or if you presented the popover from a target rectangle in a view, the popover controller does not attempt to reposition the popover. In those cases, you must manually hide the popover or present it again from an appropriate new position. You can do this in the didRotateFromInterfaceOrientation: method of the view controller that you used to present the popover.

Robot K
Ok. But what code I should write in 'didRotateFromInterfaceOrientation' method. My pop over display logic is tightly coupled with controller methods and also, my view is made up of custom UI widgets. On one such UI widget I am showing this pop over view.
Abhinav