在swift中呈现来自UITabBarItem的popover

时间:2015-12-07 04:22:53

标签: swift popover uitabbaritem

我发现很多例子展示了如何从UIBarButtonItem呈现一个popover,但我想知道如何从UITabBarItem呈现它。

这是我到目前为止所拥有的

let selectedItem = self.menuTabBar.items![3]    

let menuViewController: ActivityPopoverViewController = storyboard.instantiateViewControllerWithIdentifier("MenuViewController") as! ActivityPopoverViewController
presentViewController(menuViewController, animated: true, completion: nil)
menuViewController.modalPresentationStyle = .Popover


if let popoverMenuViewController = menuViewController.popoverPresentationController {
    popoverMenuViewController.permittedArrowDirections = .Any
    popoverMenuViewController.delegate = menuViewController


//  popoverMenuViewController.barButtonItem = ?
}

我尝试将作为UITabBarItem的selectedItem转换为UIBarButtonItem但不起作用。我确定这是一个简单的过程,但我似乎无法找到有关如何做到这一点的任何帮助。

我的目标是让这个橙色弹出窗口显示未读活动。

enter image description here

更新#1。在做了更多的研究之后,我发现了这种策略来呈现视图控制器

let tabBarItemWidth = Int(self.menuTabBar.frame.size.width) / (self.menuTabBar.items?.count)!
let x = tabBarItemWidth * 3;
let newRect = CGRect(x: x, y: 0, width: tabBarItemWidth, height: Int(self.menuTabBar.frame.size.height))
print(newRect)

let menuViewController: ActivityPopoverViewController = storyboard.instantiateViewControllerWithIdentifier("ActivityPopoverViewController") as! ActivityPopoverViewController
menuViewController.modalPresentationStyle = .Popover


if let popoverMenuViewController = menuViewController.popoverPresentationController {
    popoverMenuViewController.permittedArrowDirections = .Down
    popoverMenuViewController.delegate = menuViewController
    popoverMenuViewController.sourceRect = newRect
    popoverMenuViewController.sourceView = self.menuTabBar
    presentViewController(menuViewController, animated: true, completion: nil)

}

现在我的popover正在显示,但newRect的界限不起作用。弹出窗口占用整个屏幕而不是限制为新的边界。有谁知道如何强制新尺寸?

更新#2

工作代码

let tabBarItemWidth = Int(self.menuTabBar.frame.size.width) / (self.menuTabBar.items?.count)!
let x = tabBarItemWidth * 3;
let newRect = CGRect(x: x, y: 0, width: tabBarItemWidth, height: Int(self.menuTabBar.frame.size.height))
print(newRect)

let menuViewController: ActivityPopoverViewController = storyboard.instantiateViewControllerWithIdentifier("ActivityPopoverViewController") as! ActivityPopoverViewController
menuViewController.modalPresentationStyle = .Popover
menuViewController.view.frame = newRect
menuViewController.preferredContentSize = CGSizeMake(150, 150)

if let popoverMenuViewController = menuViewController.popoverPresentationController {
    popoverMenuViewController.permittedArrowDirections = .Down
    popoverMenuViewController.delegate = menuViewController
    popoverMenuViewController.sourceRect = newRect
    popoverMenuViewController.sourceView = self.menuTabBar

    presentViewController(menuViewController, animated: true, completion: nil)

}

2 个答案:

答案 0 :(得分:4)

工作代码

let tabBarItemWidth = Int(self.menuTabBar.frame.size.width) / (self.menuTabBar.items?.count)!
let x = tabBarItemWidth * 3;
let newRect = CGRect(x: x, y: 0, width: tabBarItemWidth, height: Int(self.menuTabBar.frame.size.height))

let menuViewController: ActivityPopoverViewController = storyboard.instantiateViewControllerWithIdentifier("ActivityPopoverViewController") as! ActivityPopoverViewController
menuViewController.modalPresentationStyle = .Popover
menuViewController.view.frame = newRect
menuViewController.preferredContentSize = CGSizeMake(150, 150)

if let popoverMenuViewController = menuViewController.popoverPresentationController {
    popoverMenuViewController.permittedArrowDirections = .Down
    popoverMenuViewController.delegate = menuViewController
    popoverMenuViewController.sourceRect = newRect
    popoverMenuViewController.sourceView = self.menuTabBar
    popoverMenuViewController.backgroundColor = UIColor.orangeColor()

    presentViewController(menuViewController, animated: true, completion: nil)
}

答案 1 :(得分:2)

//Get the views like this...
 func orderedTabBarItemViews() -> [UIView] {
        let interactionViews = tabBar.subviews.filter({$0.userInteractionEnabled})
        return interactionViews.sort({$0.frame.minX < $1.frame.minX})
    }