xcode Swift-设置可见的菜单控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26272792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:49:17  来源:igfitidea点击:

Swift- Set Visible menucontroller

xcodeswift

提问by Massimo Negro

I'm trying to display a UimenuController but I can not view it. how can I do?

我正在尝试显示 UimenuController 但我无法查看它。我能怎么做?

let MenuController: UIMenuController = UIMenuController.sharedMenuController()
 MenuController.menuVisible = true
 MenuController.arrowDirection = UIMenuControllerArrowDirection.Down
 MenuController.setTargetRect(CGRectMake(100, 80, 50, 50), inView: self.view)
 let MenuItem_1: UIMenuItem = UIMenuItem(title: "Menu", action: "delete:")
 let MenuItems: NSArray = [delete]
 MenuController.menuItems = MenuItems

回答by Aleksandr Movsesyan

In order to actually have the menu to display you need to do the following:

为了实际显示菜单,您需要执行以下操作:

  1. Call becomeFirstResponder() before you get your sharedMenuController
  2. Call menu.setMenuVisible(true, animated: true) at the end
  3. Override the canBecomeFirstResponder function
  4. Override the canPerformAction function
  5. Write the function for the selector
  1. 在获得 sharedMenuController 之前调用 becomeFirstResponder()
  2. 最后调用 menu.setMenuVisible(true, animation: true)
  3. 覆盖 canBecomeFirstResponder 函数
  4. 覆盖 canPerformAction 函数
  5. 为选择器编写函数

Here is an example

这是一个例子

func someFunc() {
    becomeFirstResponder()
    var menu = UIMenuController.sharedMenuController()
    var deleteItem = UIMenuItem(title: "Delete me", action: Selector("deleteLine"))
    menu.menuItems = [deleteItem]
    menu.setTargetRect(CGRectMake(100, 80, 50, 50), inView: self)
    menu.setMenuVisible(true, animated: true)
}

func deleteLine() {
    //Do something here
}

override func canBecomeFirstResponder() -> Bool {
    return true
}

override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
    // You need to only return true for the actions you want, otherwise you get the whole range of
    //  iOS actions. You can see this by just removing the if statement here.
    if action == Selector("deleteLine") {
        return true
    }
    return false
}

回答by mriaz0011

Swift 3 version code:

Swift 3 版本代码:

func someFunc() {
    becomeFirstResponder()
    var menu = UIMenuController.shared
    var deleteItem = UIMenuItem(title: "Delete me", action: #selector(ViewController.deleteLine))
    menu.menuItems = [deleteItem]
    menu.setTargetRect(CGRect(x: 0.0, y: 0.0, width: 20, height: 20), in: self)
    menu.setMenuVisible(true, animated: true)
}

func deleteLine() {
    //Do something here
}

override var canBecomeFirstResponder: Bool {

        return true
    }

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        // You need to only return true for the actions you want, otherwise you get the whole range of
        //  iOS actions. You can see this by just removing the if statement here.
        if action == #selector(ViewController.deleteLine) {
            return true
        }

        return false
    }

回答by Murali

One more important thing is action for UIMenuItem should be implemented.

更重要的一件事是应该实现 UIMenuItem 的操作。