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

nzkunb0c  于 2023-08-07  发布在  iOS
关注(0)|答案(1)|浏览(98)


的数据
我想知道如何实现这个“上下文菜单”,其中包含操作按钮,就像在whatsapp和其他应用程序上单击消息时出现的按钮一样。
非常感谢您的光临。

rpppsulh

rpppsulh1#

就是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];
}

相关问题