开发者

iPhone : Display popOver from tabBarItem

I wonder how I can display a popOver from a tabBarItem ? Here is how I manage my tabBarController :

tabBarController = [[UITabBarController alloc] init];

searchSplitViewController = [[UISplitViewController alloc] init];
searchRoot = [[[EI_iPad_Home_Root_ViewController alloc] init] autorelease];
searchDetail = [[[EI_iPad_Home_Detail_ViewController alloc] init] autorelease]; 
searchRootNav = [[[EI_Navigation_ViewController alloc] initWithRootViewController:searchRoot]autorelease];
searchDetailNav = [[[EI_Navigation_ViewController alloc] initWithRootViewController:searchDetail] autorelease];
searchSplitViewController.viewControllers = [NSArray arrayWithObjects:searchRootNav, searchDetailNav, nil];
searchSplitViewController.delegate = searchDetail;

favoritesSplitViewController = [[UISplitViewController alloc] init];
favoritesRoot = [[[EI_iPad_Favorites_Root_ViewController alloc] init] autorelease];
favoritesDetail = [[[EI_iPad_Favorites_GeneralDetail_ViewController alloc] init] autorelease]; 
favoritesRootNav = [[[EI_Navigation_ViewController alloc] initWithRootViewController:favoritesRoot]autorelease];
favoritesDetailNav = [[[EI_Navigation_ViewController alloc] initWithRootViewController:favoritesDetail] autorelease];
favoritesSplitViewController.viewControllers = [NSArray arrayWithObjects:favoritesRootNav, favoritesDetailNav, nil];
favoritesSplitViewController.delegate = favoritesDetail;

agencySplitViewController = [[UISplitViewController alloc] init];
agencyRoot = [[[EI_iPad_Agency_Root_ViewController alloc] init] autorelease];
agencyDetail = [[[EI_iPad_Agency_GeneralDetail_ViewController alloc] init] autorelease]; 
agencyRootNav = [[[EI_Navigation_ViewController alloc] initWithRootViewController:agencyRoot]autorelease];
agencyDetailNav = [[[EI_Navigation_ViewController alloc] initWithRootViewController:agencyDetail] autorelease];
agencySplitViewController.viewControllers = [NSArray arrayWithObjects:agencyRootNav, agencyDetailNav, nil];
agencySplitViewController.delegate = agencyDetail;

editoSplitViewController = [[UISplitViewController alloc] init];
editoRoot = [[[EI_iPad_News_Root_ViewController alloc] init] autorelease];
editoDetail = [[[EI_iPad_News_Detail_ViewController alloc] init] autorelease]; 
editoRootNav = [[[EI_Navigation_ViewController alloc] initWithRootViewController:editoRoot]autorelease];
editoDetailNav = [[[EI_Navigation_ViewController alloc] initWithRootViewController:editoDetail] autorelease];
editoSplitViewController.viewControllers = [NSArray arrayWithObjects:editoRootNav, editoDetailNav, nil];
editoSplitViewController.delegate = editoDetail;

searchSplitViewController.tabBarItem.title = NSLocalizedString(@"TabBar_search_label",nil);
searchSplitViewController.tabBarItem.image = [UIImage imageNamed:@"tabBar_search_icon.png"];

favoritesSplitViewController.tabBarItem.title = NSLocalizedString(@"TabBar_favorite_label",nil);
favoritesSplitViewController.tabBarItem.image = [UIImage imageNamed:@"tabBar_favorite_icon.png"];

agencySplitViewController.tabBarItem.title = NSLocalizedString(@"TabBar_agencies_label",nil);
agencySplitViewController.tabBarItem.image = [UIImage imageNamed:@"tabBar_agencies_icon.png"];

editoSplitViewController.tabBarItem.title = NSLocalizedString(@"TabBar_news_label",nil);
editoSplitViewController.tabBarItem.image = [UIImage imageNamed:@"tabBar_news_icon.png"];

tabBarController.viewControllers = [NSArray arrayWithObjects:searchSplitViewController,favoritesSplitViewController,agencySplitViewController,editoSplitViewController,nil];
[window addSubview:[tabBarController vi开发者_StackOverflowew]];

I want to add a fifth item which will display a popover... any idea ? Thanks


To get my UITabBarItem frame I noticed that on the iPad (on iPhone items are autorisizingWidth it's more simple) items are 75px width and are spaced by 34px.

So I do :

(tabBar.frame.size.width-((75*[tabBarController.items count])+(34*[tabBarController.items count]-1)))/2

to get the first flexible sapce width. Then I add :

(75*indexOfItemToReach)+(34*(indexOfItemToReach-1))+(75/2)

To get the width center of the item.


I would advice against this because that's not the expected behavior for a tab bar. However, you could create a UIPopoverController and display it using presentPopoverFromRect.

UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:someVC];

[pop presentPopoverFromRect:CGRectMake(100,100,100,100) permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];


You can crawl the tab bar's view hierarchy and look for UIControl subclasses (they may come up in random order, so you'll want to sort them by which one's center.x is leftmost). Each one is a tab bar button, which will have a rect.

This approach appears to be within the realm of App Store-safe coding, particularly if you fall back to just using the tab bar's frame if an OS upgrade breaks your code.

UIView has a handy undocumented method called recursiveDescription that's incredibly useful for debugging. E.g.:

(gdb) po [[[[[UIApplication sharedApplication] delegate] tabBarController] view] recursiveDescription]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜