iPhone:从tabBarItem显示popOver

时间:2011-07-04 22:29:06

标签: iphone tabbar popover

我想知道如何从tabBarItem中显示popOver? 以下是我管理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 view]];

我想添加第五个项目,它会显示一个popover ...任何想法?感谢

3 个答案:

答案 0 :(得分:1)

为了获得我的UITabBarItem框架,我注意到在iPad上(在iPhone上的项目是autorisizingWidth它更简单)项目宽度为75px,间隔为34px。

所以我这样做:

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

获得第一个灵活的sapce宽度。 然后我补充说:

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

获取项目的宽度中心。

答案 1 :(得分:0)

我建议不要这样做,因为这不是标签栏的预期行为。但是,您可以创建UIPopoverController并使用presentPopoverFromRect显示它。

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

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

答案 2 :(得分:0)

您可以抓取标签栏的视图层次结构并查找UIControl子类(它们可能以随机顺序出现,因此您需要按最左侧的center.x对其进行排序)。每一个都是一个标签栏按钮,它有一个矩形。

这种方法似乎属于App Store安全编码的范畴,特别是如果操作系统升级破坏了您的代码,您可以回到仅使用标签栏的frame

UIView有一个名为recursiveDescription的方便的无记录方法,它对调试非常有用。 E.g:

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