gpt4 book ai didi

swift - 快速呈现来自 UITabBarItem 的弹出窗口

转载 作者:搜寻专家 更新时间:2023-11-01 05:57:53 27 4
gpt4 key购买 nike

我找到了很多展示如何从 UIBarButtonItem 呈现弹出窗口的示例,但我想知道如何从 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。经过更多研究,我发现了这种呈现 View Controller 的策略

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)

}

现在我的弹出窗口正在显示,但 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)

}

最佳答案

工作代码

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)
}

关于swift - 快速呈现来自 UITabBarItem 的弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34126403/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com