LoginSignup
13
13

More than 3 years have passed since last update.

【iOS】疑似プルダウンメニューを作成する

Last updated at Posted at 2019-09-03

iOSでプルダウンメニューを作ろうと思ったら、それっぽいものが見当たらなかったので自作しました。
スクリーンショット 2019-09-03 13.44.16.png

表示元のView

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
    }
}

表示先のView

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

13
13
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13