iOS 在 UITableView 中创建 "copy paste"像弹出窗口(UIMenuController)

标签 ios iphone swift

我正在尝试创建一个类似于苹果“复制”、“粘贴”方式的难以处理的弹出窗口。我不确定该怎么做。

enter image description here

有第 3 方库还是原生组件?

最佳答案

如果对任何人有帮助,要查找的关键字是“UIMenuController”

我终于设法通过实现自定义 UITableViewCell 解决了这个问题

override func setSelected(_ selected: Bool, animated: Bool) {

        super.setSelected(selected, animated: animated)
        let showPasswordItem = UIMenuItem(title: "Show Password", action: #selector(showPass(_:)))
        let copyUserNameItem = UIMenuItem(title: "Copy Username", action: #selector(copyUsername(_:)))
        let copyPasswordItem = UIMenuItem(title: "Copy Password", action: #selector(copyPass(_:)))
        self.isPasswordShowing = !self.password.isSecureTextEntry
        UIMenuController.shared.menuItems?.removeAll()
        UIMenuController.shared.menuItems = [copyPasswordItem,copyUserNameItem,showPasswordItem,hidePasswordItem]
        UIMenuController.shared.update()

        if selected {
            self.becomeFirstResponder()
            let menu = UIMenuController.shared
            menu.setTargetRect(self.contentView.frame, in: self.contentView.superview!)
            menu.setMenuVisible(true, animated: true)
        }
    }

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {

     return  action == #selector(showPass(_:)) || action == #selector(copyUsername(_:)) || action == #selector(copyPass(_:))

}

override var canBecomeFirstResponder : Bool {
    return true
}

func showPass(_ send:AnyObject){
    self.password.isSecureTextEntry = false
    self.isPasswordShowing = true
}
func copyUsername(_ send:AnyObject){
    UIPasteboard.general.string = self.username.text
}
func copyPass(_ send:AnyObject){
    UIPasteboard.general.string = self.password.text
}
func hidePass(_ send:AnyObject){
    self.password.isSecureTextEntry = true
    self.isPasswordShowing = false
}

关于iOS 在 UITableView 中创建 "copy paste"像弹出窗口(UIMenuController),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38472461/

相关文章:

ios - 监控 ibeacon 时替代 didexitregion

ios - 找不到接受 Swift 中提供的参数的 “init” 的重载

ios - "identifierForVendor"会在 iOS 或 xCode 更新时改变吗?

ios - 按键值查询 Firebase 数据库

ios - 强制应用商店更新

iphone - 覆盖数据库或更新 (iPhone)?

iphone - 如何使用 UIGestureRecognizer 子类实现滑动

ios - 如何在 iOS 中获取或编辑视频的 exif

iphone - 使用新 'Facebook SDK for iOS' 与旧 'Facebook iPhone SDK' 进行 Facebook 身份验证

ios - Swift 4 中 strong 的搜索值