import UIKit
class ActivityPersonDetail : UIViewController,UITextFieldDelegate,UIPickerViewDataSource, UIPickerViewDelegate {
let salutations = ["1", "2", "3","4","5","6","7","8","9","10","11","12"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return salutations.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return salutations[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerTF.text = salutations[row]
picker.isHidden = true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self.pickerTF {
picker.isHidden = false
textField.endEditing(true)
}
}
lazy var picker: UIPickerView = {
let pickerView = UIPickerView()
pickerView.delegate = self
pickerView.translatesAutoresizingMaskIntoConstraints = false
pickerView.backgroundColor = .white
pickerTF.inputView = pickerView
return pickerView
}()
let arrowImage: UIImageView = {
let image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
image.image = #imageLiteral(resourceName: "down_arrow")
image.contentMode = .scaleAspectFit
return image
} ()
lazy var pickerTF: UITextField = {
let tf = UITextField()
tf.delegate = self
tf.translatesAutoresizingMaskIntoConstraints = false
tf.textColor = .black
tf.font = .systemFont(ofSize: 24, weight: .medium)
tf.placeholder = salutations[0]
return tf
}()
//
// MARK :- viewDidLoad ============================================================================
//
private var pickerView: UIPickerView!
override func viewDidLoad() {
super .viewDidLoad()
view.backgroundColor = .white
setupNavigationBar()
setupViews()
setupAutoLayout()
}
// ================ setupNavigationBar =============
func setupNavigationBar(){
title = ""
hideBackTitle()
navigationController?.navigationBar.barTintColor = .navBar
}
func hideBackTitle() {
let backbarItem = UIBarButtonItem()
backbarItem.title = ""
navigationItem.backBarButtonItem = backbarItem
}
// ================ setupViews =============
func setupViews(){
view.addSubview(pickerTF)
pickerTF.addSubview(arrowImage)
view.addSubview(picker)
picker.isHidden = true
}
// ================ setupAutoLayout =============
func setupAutoLayout() {
pickerTF.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 80).isActive = true
pickerTF.topAnchor.constraint(equalTo: view.topAnchor, constant: 80).isActive = true
pickerTF.heightAnchor.constraint(equalToConstant: 60).isActive = true
pickerTF.widthAnchor.constraint(equalToConstant: 70).isActive = true
arrowImage.rightAnchor.constraint(equalTo: pickerTF.rightAnchor, constant: -15).isActive = true
arrowImage.topAnchor.constraint(equalTo: pickerTF.topAnchor, constant: 16).isActive = true
arrowImage.widthAnchor.constraint(equalToConstant: 22).isActive = true
arrowImage.heightAnchor.constraint(equalToConstant: 22).isActive = true
picker.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
picker.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
picker.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
picker.heightAnchor.constraint(equalToConstant: 100).isActive = true
}
}