【问题标题】:UITabBarController with a UIPopOverController带有 UIPopOverController 的 UITabBarController
【发布时间】:2012-01-31 13:04:22
【问题描述】:

我有一个要求,我需要从 UITabBarController 实现 UIPopOverController,即在 UITabBarController 中,当我单击一个名为“pop”的选项卡时,弹出框应该显示两个字段。我的问题是,实现这一目标的最佳方法是什么。如果有任何示例视频或只是一些解释材料,那么您也可以与我分享链接。请忍受我,因为我在 ios 环境中弄湿了我的脚。

任何建议将不胜感激!

谢谢 马克斯

【问题讨论】:

  • 这对于系统控件来说是一个比较出乎意料的行为;不要忘记阅读 HIG 并尝试问问自己在用户体验方面是什么样的......

标签: ios ipad xcode4.2


【解决方案1】:

我整理了一个示例项目,它将在选定的 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];
    }
}

【讨论】:

  • 非常感谢您的源代码和MobileOverlord的快速回复
  • 我有一个问题,现在 PopOverController 显示两个 TabBarItems,我怎样才能限制只有一个 TabBarItems 例如说只有 SecondViewController,而不是在两个视图控制器上弹出
  • 我已经更新了我的答案,以说明如何根据选择的 viewController 有选择地执行此操作。
  • 非常感谢MobileOverlord,你提供的信息真的很丰富,也让我前进。
【解决方案2】:

这是我的解决方案,我在“https://github.com/mobileoverlord/UITabBarPopOver-Demo”的基础上进行了以上修改,下面是我的一个Demo。 https://github.com/mrhyh/iPad/tree/master/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多