UIMenu 不弹出
Posted
技术标签:
【中文标题】UIMenu 不弹出【英文标题】:UIMenu doesn't pop up 【发布时间】:2021-04-10 12:32:09 【问题描述】:一旦用户按下正确的UIBarButton
,我就会尝试弹出一个UIMenu
,但问题是 UIMenu 没有弹出,我不知道为什么,如果你们可以的话会很酷帮帮我。
我还添加了注释代码,向您展示所有尝试。
我的问题是当我设置 UIBarButton
但不确定时。顺便提一句!我没有使用计算属性,因为当我使用这种方式时我无法编辑它们,所以我只使用了一个普通的存储变量。
我的代码:
// MARK: - UIBarButtonItem Creation and Configuration
func configureUIButtons()
// let toolbarButtonItems = [
// customTitleBarButtonItem,
// flexibleSpaceBarButtonItem,
// deleteBarButtonItem
// ]
let toolbarButtonItems = [
markBarButtonItem, flexibleSpaceBarButtonItem, deleteBarButtonItem
]
toolBar.setItems(toolbarButtonItems, animated: true)
var deleteBarButtonItem: UIBarButtonItem = UIBarButtonItem(title: "Delete", style: .plain, target: self, action: #selector(NotificationsViewController.deleteButtonItemClicked(_:)))
// var deleteBarButtonItem: UIBarButtonItem
// let deleteBarButtonItem: UIBarButtonItem = UIBarButtonItem(title: "Delete", style: .plain, target: self, action: #selector(NotificationsViewController.deleteButtonItemClicked(_:)));
//
// return deleteBarButtonItem
//
//
var flexibleSpaceBarButtonItem: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
// var flexibleSpaceBarButtonItem: UIBarButtonItem
// return UIBarButtonItem(barButtonSystemItem: .flexibleSpace,
// target: nil,
// action: nil)
//
var markBarButtonItem: UIBarButtonItem = UIBarButtonItem(title: "Mark", style: .plain, target: self, action: #selector(NotificationsViewController.markButtonItemClicked(_:)))
// MARK: - Actions
@objc
func deleteButtonItemClicked(_ barButtonItem: UIBarButtonItem)
print("Delete Button is clicked \(barButtonItem).")
@objc
func markButtonItemClicked(_ barButtonItem: UIBarButtonItem ) -> UIMenu
print("Mark is pressed")
let markMenu = UIMenu(title: "", children: [
UIAction(title: "Flag", image: UIImage(systemName: "flag")) action in
print("Flag is pressed")
,
UIAction(title: "Mark as Unread", image: UIImage(systemName: "envelope.badge")) action in
print("Mark as Unread is pressed")
,
])
return markMenu
// func menuItems() -> UIMenu
// let markMenu = UIMenu(title: "", children: [
// UIAction(title: "Flag", image: UIImage(systemName: "flag")) action in
// print("Flag is pressed")
// ,
// UIAction(title: "Mark as Unread", image: UIImage(systemName: "envelope.badge")) action in
// print("Mark as Unread is pressed")
// ,
// ])
//
// return markMenu
//
//
【问题讨论】:
【参考方案1】:您没有为菜单使用正确的初始化程序。
let markMenu = UIMenu(title: "", children: [
UIAction(title: "Flag", image: UIImage(systemName: "flag")) action in
print("Flag is pressed")
,
UIAction(title: "Mark as Unread", image: UIImage(systemName: "envelope.badge")) action in
print("Mark as Unread is pressed")
,
])
let markBarButtonItem = UIBarButtonItem(title: "Mark", image: nil, primaryAction: nil, menu: markMenu)
let toolBar = UIToolbar(frame: CGRect(origin: .zero, size: CGSize(width: 300, height: 100)))
toolBar.setItems([markBarButtonItem], animated: true)
【讨论】:
我明白了,会试试,顺便说一句 UIMenu 仅适用于 ios 14 用户?有什么可做的吗?我的意思是,如果我有使用 iOS 12 的用户,我会怎么做? ?【参考方案2】:在bar按钮初始化中添加选择器时使用“self”而不是视图控制器的名称(NotificationsViewController),或者如果您不喜欢放置self,则直接引用函数名称。不需要时放在方法和属性的前面。
【讨论】:
以上是关于UIMenu 不弹出的主要内容,如果未能解决你的问题,请参考以下文章