【问题标题】:How can I find the UIView for a specific UITabBarItem?如何找到特定 UITabBarItem 的 UIView?
【发布时间】:2016-03-07 05:16:42
【问题描述】:

我正在使用一个名为 SwiftyWalkthrough 的外部库,它允许我仅在屏幕上显示某些视图,以引导新用户通过我的应用程序的 UI。我想向用户展示的第一个项目是 UITabBarItem。我需要找到与该特定 UITabBarItem 关联的 UIView。我知道该项目的索引,并且给了它一个标签。但我还没有找到让他们帮助我找到风景的方法。当然,我只想使用公共接口来完成这个。

【问题讨论】:

  • 标签栏项中有很多视图。你在找哪一个?
  • 屏幕底部标签栏上的标题和图标。我希望它是矩形的,高度与整个标签栏相同,宽度等于标签栏宽度除以标签栏项目的数量。
  • 我觉得是无法访问UITabbaritem backgroundView,只能放tab bar item的自定义图片,而无法获取tabbar item的backgroundView。

标签: ios swift uiview uitabbaritem


【解决方案1】:

我通过访问 tabBar 的子视图解决了我的问题。具有 userInteractionEnabled 的视图是 UITabBarItems。按它们的 minX 值对它们进行排序可确保它们与 tabBar.items 数组的顺序相同。

extension UITabBarController {
    func orderedTabBarItemViews() -> [UIView] {
        let interactionViews = tabBar.subviews.filter({$0.isUserInteractionEnabled})
    return interactionViews.sorted(by: {$0.frame.minX < $1.frame.minX})
    }
}

【讨论】:

  • 谢谢!真的很好很优雅。我知道这可能会在未来中断,但似乎没有更好的方法......
【解决方案2】:

为 Carl Smith 的回答的 Swift 3 版本

func orderedTabBarItemViews() -> [UIView] {
    let interactionViews = tabBar.subviews.filter({$0.isUserInteractionEnabled})
    return interactionViews.sorted(by: {$0.frame.minX < $1.frame.minX})
}

【讨论】:

    【解决方案3】:

    在 iOS10 中使用 Objective-C 测试:

    [alert setModalPresentationStyle:UIModalPresentationPopover];
    
        UIView *view = [self.tabBar.selectedItem valueForKey:@"view"];
    
        alert.popoverPresentationController.delegate = self;
        alert.popoverPresentationController.sourceView = self.tabBar;
        alert.popoverPresentationController.sourceRect = view.frame;
        alert.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionDown;
    
        [self presentViewController:alert animated:YES completion:nil];
    

    【讨论】:

    • 斯威夫特if let view = tabBar.selectedItem?.value(forKey: "view") as? UIView { }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多