我放在一起,將顯示所選擇的UITabBarItem上方UIPopoverController的示例項目。
http://mobileoverlord.com/displaying-a-uipopovercontroller-from-a-uitabbaritem/
這包含了一點兩輪牛車,因爲你需要通過的TabBar的子視圖進行迭代。此外,它可能會在iOS 5上不同,因爲TabBar的背景視圖在TabBar的子視圖數組中。它在tabBarController委託方法中實現
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSInteger index = [[self tabBarController] selectedIndex];
CGRect buttonFrame = [[[[[self tabBarController] tabBar] subviews] objectAtIndex:index+1] frame];
PopOverViewController *popoverView = [PopOverViewController new];
popoverView.contentSizeForViewInPopover = CGSizeMake(250, 300);
popover = [[UIPopoverController alloc]initWithContentViewController:popoverView];
NSLog(@"X:%f Y:%f",buttonFrame.origin.x,buttonFrame.origin.y);
[popover presentPopoverFromRect:buttonFrame inView:self.tabBarController.tabBar permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
以下是示例代碼。隨意評論和操縱。
https://github.com/mobileoverlord/UITabBarPopOver-Demo
如果你想將它限制爲僅當某個按鈕被按下,你可以過濾器的類傳入viewController
像這樣
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([viewController isKindOfClass:[SecondViewController class]]) {
NSInteger index = [[self tabBarController] selectedIndex];
CGRect buttonFrame = [[[[[self tabBarController] tabBar] subviews] objectAtIndex:index+1] frame];
PopOverViewController *popoverView = [PopOverViewController new];
popoverView.contentSizeForViewInPopover = CGSizeMake(250, 300);
popover = [[UIPopoverController alloc]initWithContentViewController:popoverView];
NSLog(@"X:%f Y:%f",buttonFrame.origin.x,buttonFrame.origin.y);
[popover presentPopoverFromRect:buttonFrame inView:self.tabBarController.tabBar permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
}
這對系統控制來說是一個相對意想不到的行爲;不要忘了閱讀HIG,並試着問自己在用戶體驗方面是什麼樣的...... – 2012-02-01 03:23:31