UIPopoverController placement

给你一囗甜甜゛ 提交于 2020-01-21 10:28:30

问题


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:


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;


来源:https://stackoverflow.com/questions/5278236/uipopovercontroller-placement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!