iOS 14在打开之前设置UIBarButttonItem菜单 [英] iOS 14 setup the UIBarButttonItem menu before it is opened

查看:138
本文介绍了iOS 14在打开之前设置UIBarButttonItem菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于iOS14提供了一种简便的方法来设置UIBarButtonItem上的菜单,我想知道我没有想到没有简单的方法来设置其项目,因为它们可能取决于当前上下文.

我看到有UIDeferredMenuElement,但这似乎不是一个好用例?

我也看到有primaryAction,但是似乎没有一种从那里调用菜单的简单方法.

第三个选项似乎是在每次可能影响它的更改上重新组织菜单,但这似乎不是一个好主意.

我认为有些事情我错过了.

解决方案

基本上,对于简单的条形按钮项,没有这样的规定.类似于UIButton的UIControl带有事件.menuActionTriggered,在那时,构造菜单并将其分配给控件是完全合理的,因此实现了动态菜单,该菜单是在菜单出现之前即刻构造的. /p>

但是您不能使用条形按钮项执行此操作-除非它是其中包含UIButton的自定义视图条形按钮项.

否则,您需要做的是通过某种 other 机制观察周围的情况,并在每次条件改变时只需更改条形按钮项目的菜单.听起来像是屁股疼,但这可能是您唯一的选择. (我想这就是您说的意思,第三个选项似乎是在可能影响它的每个更改上重新组合菜单,但这似乎不是一个好主意.")

我认为您的要求是合理的,没有准备金似乎有点可笑;如果您有很好的用例,建议您向Apple提出增强请求.

As iOS14 offers an easy way to setup the menu on the UIBarButtonItem, I wonder do I miss that there is no easy way to setup its items as they may depend on the current context.

I see that there is UIDeferredMenuElement, but this doesn't seem to be a good use case for it?

Also I see that there is primaryAction, but there doesn't seem to be a trivial way to call the menu from there.

And the third option seems to be reshuffling the menu on each change that may affect it, but that doesn't seem like a great idea.

I assume that there is something I miss out.

解决方案

Basically for a simple bar button item there's no such provision. A UIControl like a UIButton has an event .menuActionTriggered, and it is perfectly reasonable, at that moment, to construct and assign the menu to the control, thus implementing a dynamic menu that is constructed just in the instant before the menu appears.

But you can't do that with a bar button item — unless it is a custom view bar button item with a UIButton in it.

What you'd have to do otherwise is watch the surrounding conditions via some other mechanism and just change the bar button item's menu each time the conditions change. It sounds like a pain in the butt but it's probably your only option. (I take it that that's exactly what you mean when you say "And the third option seems to be reshuffling the menu on each change that may affect it, but that doesn't seem like a great idea.")

I think what you're asking for is reasonable and it seems sort of silly that there's no provision for it; if you have a good use case, I'd recommend filing an enhancement request with Apple.

这篇关于iOS 14在打开之前设置UIBarButttonItem菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆