0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

キーボードの表示をPickerViewにする方法

Last updated at Posted at 2020-12-09

キーボードのデフォルト表示をinputViewを使って表示してみましょう!

コードはこんな感じです!

sampleViewController.swift
import UIKit

class sampleViewController: UIViewController {
    
    @IBOutlet weak var PickerTextFiled: UITextField!
    let PickerView = UIPickerView()
    let PickerList = ["りんご", "ばなな", "ぶどう", "すいか",  "かき",  "なし"]
    override func viewDidLoad() {
        super.viewDidLoad()
        setupProjectPickerView()
    }
    
    
    
    func setupProjectPickerView() {
           PickerView.dataSource = self
           PickerView.delegate = self
           PickerView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: PickerView.bounds.size.height)
           let PickerV = UIView(frame: PickerView.bounds)
           PickerV.backgroundColor = UIColor.white
           PickerV.addSubview(PickerView)
           
           // キーボードをpickerViewに!!
           PickerTextFiled.inputView = PickerV
       }
  
}

extension sampleViewController: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return PickerList.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return PickerList[row]
    }
    
    
}

PickerTextFiled.inputView = PickerV
これで設定が可能です!

スクリーンショット 2020-12-09 18.42.04.png

こんなふうにデフォルトで表示することができましたね!

0
1
0

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?