[iOS] 드롭다운 의심 메뉴 만들기

14141 단어 iOSSwift
iOS로 드롭다운 메뉴를 만들려고 할 때 그 모양을 찾을 수 없어서 자제했어요.

소스 보기

import UIKit

class ViewController: UIViewController {

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

    @IBAction func tappedButton(_ sender: UIButton) {
        let frame = sender.frame
        presentPopoverView(frame: frame)
    }

    func presentPopoverView(frame: CGRect){
        let contentVC = ContentViewController()

        contentVC.modalPresentationStyle = .popover
        contentVC.preferredContentSize = CGSize(width: 100, height: 200)

        guard let popoverPresentationController = contentVC.popoverPresentationController else { return }

        popoverPresentationController.sourceView = view
        popoverPresentationController.sourceRect = frame
        popoverPresentationController.permittedArrowDirections = .any
        popoverPresentationController.delegate = self

        present(contentVC, animated: true, completion: nil)
    }

}

extension ViewController: UIPopoverPresentationControllerDelegate {
    // iPhoneで表示させる場合に必要
    func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return .none
    }
}

대상 보기

import UIKit

class ContentViewController: UIViewController {

    let cellHeight: CGFloat = 50
    let array = ["1", "10", "100", "1000", "10000", "100000"]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewDidLayoutSubviews() {
        setTableView(view: view)
    }

    func setTableView(view: UIView){
        let posX: CGFloat = 0
        let posY: CGFloat = 0

        let width: CGFloat = view.frame.width
        let height: CGFloat = view.frame.height

        let tableView = UITableView(frame: CGRect(x: posX, y: posY, width: width, height: height))

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "tableViewCell")
        tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        tableView.bounces = false
        tableView.showsVerticalScrollIndicator = false

        tableView.delegate = self
        tableView.dataSource = self

        view.addSubview(tableView)
    }

}

extension ContentViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return cellHeight
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(array[indexPath.row])
    }
}

extension ContentViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath)

        cell.textLabel?.text = array[indexPath.row]
        cell.textLabel?.textAlignment = .center

        return cell
    }
}

후기


제가 이 보도를 참고하도록 허락해 주십시오.
https://qiita.com/orimomo/items/1a44337de974a72b6266

좋은 웹페이지 즐겨찾기