【问题标题】:iOS 11 not able to disable copy paste option UITextFieldiOS 11 无法禁用复制粘贴选项 UITextField
【发布时间】:2018-03-20 15:03:58
【问题描述】:

我正在使用 Xcode 9 和 iOS 11,谷歌搜索禁用复制粘贴选项但没有成功。

低于我的尝试:

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(UIResponderStandardEditActions.copy(_:)) {
        return false
    }

    return true
}

也试过了:

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(paste(_:)) || action == #selector(cut(_:)) || action == #selector(selectAll(_:)) || action == #selector(select(_:)){
            return false
        }

        return true
    }

并尝试了Stackoverflow 的许多其他代码。但没有获得成功。其实我不想显示上面的菜单然后长按UITextField

【问题讨论】:

  • 你需要override var canBecomeFirstResponder: Bool { return true }
  • 这个方法会被调用吗?
  • 我检查了你的代码,它在 iOS 11 中运行
  • @NikhilManapure - 是的
  • @Rajesh - 你没有看到上面的选择菜单把它作为图像吗?

标签: ios swift cocoa-touch uitextfield ios11


【解决方案1】:

Swift 5 以下代码适用于我

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    OperationQueue.main.addOperation {
        UIMenuController.shared.setMenuVisible(false, animated: false)
    }
    return super.canPerformAction(action, withSender: sender)
}

【讨论】:

    【解决方案2】:

    在 iOS 12 中测试和工作:

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool
    {
        if  action == #selector(self.paste(_:))
            ||
            action == #selector(self.copy(_:))
            ||
            action == #selector(self.cut(_:))
            ||
            action == #selector(self.select(_:))
            ||
            action == #selector(self.selectAll(_:))
    
        {
            return false
        }
        return super.canPerformAction(action, withSender: sender)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 2017-07-15
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多