UIPopoverController placement

2019-02-21 04:51发布

I'm working on an app that is supposed to be universal, one app for both iPad and iPhone. I would like to keep their interfaces as similar as possible. In the iPhone app I am using a Tab bar controller, and one of those tabs goes to an image picker controller. Obviously I cannot do that in iPad. So I have hijacked control of that button to bring a popupcontroller that has the image picker in it. This all works well enough, except when I bring up the popup, it is not in the correct place. When I rotate the simulator, the popup goes to the correct place, and stays when I rotate back even.

My code is based on the code in this question: Ipad UIImagePickerController and UIPopoverController error

Why would my popup not be in the correct location?

1条回答
冷血范
2楼-- · 2019-02-21 05:00

If your code is based on the question you referenced, it would appear you are using the following code to show the popover:

[popoverController presentPopoverFromBarButtonItem:sender
                          permittedArrowDirections:UIPopoverArrowDirectionUp 
                                          animated:YES]

UIPopoverController:presentPopoverFromBarButtonItem:permittedArrowDirections:animated accepts a UIBarButtonItem* for the sender which your UITabBar does not have. UITabBar uses UITabBarItem which has a base of UIBarItem. UIBarButtonItem also has that base (UIBarItem).

Anyhow... I also needed to show a uipopovercontroller from a tabbaritem, I used the following code:

MyViewController *myVC = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:myVC];
[myVC release];
popover.popoverContentSize = myVC.view.frame.size;
popover.delegate = self;
int tabBarItemWidth = self.tabBar.frame.size.width / [self.tabBar.items count];
int x = tabBarItemWidth * 6;
CGRect rect = CGRectMake(x, 0, tabBarItemWidth, self.tabBar.frame.size.height);
[popover presentPopoverFromRect:rect
                         inView:self.tabBar 
       permittedArrowDirections:UIPopoverArrowDirectionUp 
                       animated:YES];

Note: your x calculation will be different. The tab bar item selected for me was the 6th one. Basically

x = tabBarItemWidth * currentTabBarItemIndex;
查看更多
登录 后发表回答