UIMenuController在Tableview中不可见

时间:2018-08-17 09:27:14

标签: ios iphone swift xcode uitableview

在表格单元格上长按手势时,我无法显示自定义MenuController。

我的代码如下:

 override func viewDidLoad() {
        super.viewDidLoad()
        self.setupLongPressGesture()
    }

func setupLongPressGesture() {
        let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress))
        self.tbl.addGestureRecognizer(longPressGesture)
    }

    @objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer){
        if gestureRecognizer.state == .began {
            let touchPoint = gestureRecognizer.location(in: self.tbl)
            if let indexPath = tbl.indexPathForRow(at: touchPoint) {

                let cell = tbl.cellForRow(at: indexPath) as! ChattingTextCell
                self.view.becomeFirstResponder()                
                let copy = UIMenuItem(title: "piy", action: #selector(copy_text))

                let menucontroller = UIMenuController.shared
                menucontroller.menuItems = [copy]
                menucontroller.update()
                //menucontroller.setTargetRect(cell.frame, in: tbl)
                menucontroller.setTargetRect(CGRect(x: 100, y: 200, width: 100, height: 50), in: self.view)
                menucontroller.setMenuVisible(true, animated: true)

            }
        }
    }


     func canBecomeFirstResponder() -> Bool {
        return true
    }



    func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool
    {
        print(action)
        return true
    }

我已经提到过很多站点,但是所有站点都在说同样的话...我也找不到任何好的教程... 请帮助我解决这个问题。...

0 个答案:

没有答案