请告诉我如何定制这个菜单?也许还有另一种方法可以做到这一点?
let barMenu = UIMenu(title: "", children: [
UIAction(title: NSLocalizedString("menu_item_home", comment: "")){
action in
print("menu_item_home 1")
},
UIAction(title: NSLocalizedString("menu_item_settings", comment: "")){
action in
print("menu_item_settings 2")
let settingsStoryboard = UIStoryboard(name: "Settings", bundle: nil)
let settingsController = settingsStoryboard.instantiateViewController(withIdentifier: "SettingsScene") as! SettingsViewController
controller.navigationController?.pushViewController(settingsController, animated: true)
},
UIAction(title: NSLocalizedString("menu_item_contacts", comment: "")){
action in
print("menu_item_contacts 3")
},
])
let navBarMenu = UIBarButtonItem(image: UIImage(systemName: "text.justify"), menu: barMenu)
navigationItem.rightBarButtonItem = navBarMenu
Run Code Online (Sandbox Code Playgroud)
我需要向导航栏添加一个菜单并自定义其外观。请指出正确的方向
目标: 尝试创建一个过滤器选择屏幕(在卡片视图中),其中包含带有 UIMenu 项目的 UIButtons。当我选择任何过滤器并单击“应用过滤器”按钮时,我会关闭过滤器视图控制器并返回到集合视图屏幕以使用所选过滤器刷新数据。
问题: 在已选择过滤器的情况下,当我想再次返回以更改任何过滤器时,我单击过滤器按钮以打开过滤器视图控制器,但显然没有保留任何 UIMenu 选择,因为它们首先重置为默认值索引设置。
如何解决?: 当我的数据从filterVC传回主列表屏幕时,我传递了所选UIMenu的标题。我想我可以将其传递回filterVC并将菜单默认为传入的数据(即可以搜索标题并使其成为UIMenu中选定的UIAction),但是我似乎找不到一种方法手动选择哪个 UIAction 应该是 UIMenu 的选定状态!
var spaceTypeMenu: UIMenu {
return UIMenu(title: "Space Type", image: nil, identifier: nil, options: [], children: spaceTypeMenuItems)
}
private var spaceTypeMenuItems: [UIAction] {
return [
UIAction(title: "All", handler: { (_) in }),
UIAction(title: "Meeting Room", handler: { (_) in }),
UIAction(title: "Desk", handler: { (_) in }),
UIAction(title: "Boardroom", handler: { (_) in })
]
}
Run Code Online (Sandbox Code Playgroud)
然后下面是从viewdidload()中调用的
func createSpaceTypeMenu() {
spaceTypeButton?.menu = spaceTypeMenu
spaceTypeButton?.showsMenuAsPrimaryAction = true …
Run Code Online (Sandbox Code Playgroud) 在 iOS 13/14 中,有一种简单的方法可以通过以下方式呈现上下文菜单UIContextMenuInteraction
:
anyUIView.addInteraction(UIContextMenuInteraction(delegate: self))
Run Code Online (Sandbox Code Playgroud)
对我来说,问题在于它模糊了整个用户界面。此外,这只能通过长按/触觉触摸来调用。
如果我不想模糊,可以使用操作菜单。如此处所示 https://developer.apple.com/documentation/uikit/menus_and_shortcuts/adopting_menus_and_uiactions_in_your_user_interface
这似乎没有模糊地呈现,但它似乎只附加到 aUIButton
或 a UIBarButtonItem
。
let infoButton = UIButton()
infoButton.showsMenuAsPrimaryAction = true
infoButton.menu = UIMenu(options: .displayInline, children: [])
infoButton.addAction(UIAction { [weak infoButton] (action) in
infoButton?.menu = infoButton?.menu?.replacingChildren([new items go here...])
}, for: .menuActionTriggered)
Run Code Online (Sandbox Code Playgroud)
有没有办法将上下文菜单附加到长按时调用且不会模糊显示的 UIView 上?
iOS 14 中的新功能,我们可以将动作处理程序直接附加到 UIControl:
let action = UIAction(title:"") { action in
print("howdy!")
}
button.addAction(action, for: .touchUpInside)
Run Code Online (Sandbox Code Playgroud)
这很酷,但语法令人愤怒。我必须先形成 UIAction。我必须给 UIAction 一个标题,即使该标题永远不会出现在界面中。没有更好的办法吗?
uiaction ×4
ios ×3
swift ×3
uimenu ×2
contextmenu ×1
ios14 ×1
uicontrol ×1
uikit ×1
uimenuitem ×1
xcode ×1