Skip to content

Instantly share code, notes, and snippets.

@hectorddmx
Created October 18, 2019 17:30
Show Gist options
  • Save hectorddmx/eb271effe646e7a61f1dfa9a6db75dbd to your computer and use it in GitHub Desktop.
Save hectorddmx/eb271effe646e7a61f1dfa9a6db75dbd to your computer and use it in GitHub Desktop.
Custom UITextfield with picker
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class CustomTextField: UITextField {
weak var presenter: UIViewController!
override func becomeFirstResponder() -> Bool {
let alertSheet = UIAlertController(title: "Opciones", message: "Mensaje", preferredStyle: .actionSheet)
let buttonOneAction = UIAlertAction(title: "Opcion uno", style: .default) { action in
// Select item
}
let buttonTwoAction = UIAlertAction(title: "Opcion uno", style: .default) { action in
// Select item
}
alertSheet.addAction(buttonOneAction)
alertSheet.addAction(buttonTwoAction)
return true
}
}
struct FieldsRepository {
var fields: [TextFieldViewModel]
}
struct TextFieldViewModel: Identifiable {
var id: UUID
var textFieldName: String
var placeHolder: String
var validations: [()->Bool]?
var lengthValidationRange: Range<Int>
}
class PickerTextfieldCollectionViewCell: UICollectionViewCell, UITextFieldDelegate {
var textFieldViewModel: TextFieldViewModel!
private lazy var fieldStackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.alignment = .center
stackView.distribution = .fillProportionally
return stackView
}()
private lazy var pickerTextfieldLabel: UILabel = {
let label = UILabel()
label.textColor = .blue
label.font = UIFont.systemFont(ofSize: 17, weight: .bold)
return label
}()
private lazy var pickerTextField: CustomTextField = {
let textField = CustomTextField()
return textField
}()
private lazy var overlayButton: UIButton = {
let button = UIButton()
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.configureViews()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
self.configureViews()
}
override func prepareForReuse() {
pickerTextField.text = ""
pickerTextField.placeholder = ""
}
private func configureViews() {
pickerTextField.delegate = self
self.fieldStackView.addArrangedSubview(pickerTextfieldLabel)
self.fieldStackView.addArrangedSubview(pickerTextField)
self.addSubview(self.fieldStackView)
// self.addSubview(self.overlayButton)
NSLayoutConstraint.activate([
self.fieldStackView.topAnchor.constraint(equalTo: self.topAnchor),
self.fieldStackView.bottomAnchor.constraint(equalTo: self.bottomAnchor),
self.fieldStackView.leftAnchor.constraint(equalTo: self.leftAnchor),
self.fieldStackView.rightAnchor.constraint(equalTo: self.rightAnchor),
// self.overlayButton.topAnchor.constraint(equalTo: self.topAnchor),
// self.overlayButton.bottomAnchor.constraint(equalTo: self.bottomAnchor),
// self.overlayButton.leftAnchor.constraint(equalTo: self.leftAnchor),
// self.overlayButton.rightAnchor.constraint(equalTo: self.rightAnchor),
])
}
func configureCellData(viewModel: TextFieldViewModel) {
pickerTextField.placeholder = textFieldViewModel.placeHolder
}
// MARK: - UITextFieldDelegate
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return true
}
}
class MyViewController : UIViewController {
private lazy var contentStackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.alignment = .fill
stackView.distribution = .fill
return stackView
}()
private lazy var collectionView: UICollectionView = {
let collectionView = UICollectionView()
return collectionView
}()
override func loadView() {
let view = UIView()
view.backgroundColor = .white
view.addSubview(contentStackView)
NSLayoutConstraint.activate([
contentStackView.topAnchor.constraint(equalTo: view.topAnchor),
contentStackView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
contentStackView.leftAnchor.constraint(equalTo: view.leftAnchor),
contentStackView.rightAnchor.constraint(equalTo: view.rightAnchor),
])
self.view = view
}
}
// Present the view controller in the Live View window
let myVC = MyViewController()
myVC.preferredContentSize = CGSize(width: 300, height: 100)
PlaygroundPage.current.liveView = myVC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment