美文网首页iOS进阶+实战
iOS16适配指南之UIEditMenuInteraction

iOS16适配指南之UIEditMenuInteraction

作者: YungFan | 来源:发表于2022-08-18 09:49 被阅读0次
    • iOS 16 中新增的菜单交互,用于取代 UIMenuController 与 UIMenuItem。
    • 目前 UITextField 与 UITextView 已经默认支持。
    //  Created by YungFan
    import UIKit
    
    class ViewController: UIViewController {
        lazy var textField: UITextField = {
            let textField = UITextField(frame: CGRect(x: 0, y: 500, width: 300, height: 44))
            textField.borderStyle = .roundedRect
            textField.delegate = self
            return textField
        }()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            view.addSubview(textField)
        }
    }
    
    // MARK: - UITextFieldDelegate
    extension ViewController: UITextFieldDelegate {
        // MARK: 通过代理方法添加交互的菜单
        func textField(_ textField: UITextField,
                       editMenuForCharactersIn range: NSRange,
                       suggestedActions: [UIMenuElement]) -> UIMenu? {
            let favorite = UIAction(title: "Favorite") { _ in
                print("favorite")
            }
            let share = UIAction(title: "Share") { _ in
                print("share")
            }
            let delete = UIAction(title: "Delete", attributes: [.destructive]) { _ in
                print("delete")
            }
            return UIMenu(children: [favorite, share, delete])
        }
    }
    

    相关文章

      网友评论

        本文标题:iOS16适配指南之UIEditMenuInteraction

        本文链接:https://www.haomeiwen.com/subject/ajcegrtx.html