Adding a done button to hide after use.
tesddev opened this issue · 0 comments
tesddev commented
I did something like this to be able to use it in my project
//
// SwipeCalendarView.swift
// Created by Tes on 03/03/2023.
//
import UIKit
import MDatePickerView
class SwipeCalendarView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
translatesAutoresizingMaskIntoConstraints = false
backgroundColor = UIColor(hexString: "#F2F6F8")
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
lazy var calendarView : MDatePickerView = {
let mdate = MDatePickerView()
mdate.Color = .black
mdate.from = 1980
mdate.to = Calendar(identifier: .gregorian).dateComponents([.year], from: Date()).year ?? 0
mdate.translatesAutoresizingMaskIntoConstraints = false
return mdate
}()
lazy var doneButton: AppButton = {
let button = AppButton()
button.setTitle("Done", for: .normal)
button.addTarget(self, action: #selector(didTapDoneButton), for: .touchUpInside)
return button
}()
override func layoutSubviews() {
super.layoutSubviews()
self.addSubview(calendarView)
self.addSubview(doneButton)
NSLayoutConstraint.activate([
calendarView.heightAnchor.constraint(equalToConstant: 300),
calendarView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.9),
calendarView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
calendarView.topAnchor.constraint(equalTo: self.topAnchor, constant: 20),
doneButton.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -20),
doneButton.centerXAnchor.constraint(equalTo: self.centerXAnchor),
doneButton.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.8),
doneButton.heightAnchor.constraint(equalToConstant: 40),
])
}
@objc func didTapDoneButton() {
self.isHidden = true
}
}
then to use in the view controller needed -
declared it -
private let calendarParentView: SwipeCalendarView = {
let view = SwipeCalendarView()
view.backgroundColor = .white
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 16
view.layer.borderWidth = 1
view.layer.borderColor = UIColor.darkGray.cgColor
view.isHidden = true
return view
}()
then set it's delegate to self in the viewdidload
calendarParentView.calendarView.delegate = self
and now it works like this -
done.button.+.datePicker.demo.mp4
then bring it to display when i tap on certain textfield, in my case, a date of birth textfield.
It would be nice if you can add the feature to your project.