Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a done button to hide after use. #21

Open
tesddev opened this issue Mar 3, 2023 · 0 comments
Open

Adding a done button to hide after use. #21

tesddev opened this issue Mar 3, 2023 · 0 comments

Comments

@tesddev
Copy link

tesddev commented Mar 3, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant