iOS16适配指南之UIEditMenuInteraction

2022-08-23 15:32:07 浏览数 (1)

  • iOS 16 中新增的菜单交互,用于取代 UIMenuController 与 UIMenuItem。
  • 目前 UITextField 与 UITextView 已经默认支持。
代码语言:javascript复制
//  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])
    }
}

0 人点赞