如何在 iOS 中将此菜单添加到应用程序中?菜单类似于 iPhone 中的文本字段?

问题描述 投票:0回答:1

我想知道如何使用操作按钮(例如单击消息时出现在 Whatsapp 和其他应用程序上的操作按钮)来实现此“上下文菜单”。

非常感谢。

ios swift xcode contextmenu uimenucontroller
1个回答
10
投票

那就是

UIMenuController

斯威夫特

    let contextMenu = UIMenuController.shared
    contextMenu.menuItems = [
    UIMenuItem(title: "test", action: #selector(testclicked)),
    UIMenuItem(title: "test 1", action: #selector(test1clicked)),
    UIMenuItem(title: "test2", action: #selector(test2clicked)),
    UIMenuItem(title: "test3", action: #selector(test3clicked))
    ]
    contextMenu.showMenu(from: self.view, rect: CGRect(x: 100, y: 190, width: 100, height: 20))

目标C

- (void)showMenu
{
    UIMenuController *menu = [UIMenuController sharedMenuController];
    menu.menuItems = @[
       [[UIMenuItem alloc] initWithTitle:@"Title1" action:@selector(MyAction1)],
       [[UIMenuItem alloc] initWithTitle:@"Title2" action:@selector(MyAction2)],
       [[UIMenuItem alloc] initWithTitle:@"Title3" action:@selector(MyAction)]];
    [menu setTargetRect:self.bounds inView:self];
    [menu setMenuVisible:YES animated:YES];
}

更新:请立即使用

UIEditMenuInteraction
,因为
UIMenuController
已弃用

© www.soinside.com 2019 - 2024. All rights reserved.