How to accept only Month and Year using DatePicker in iOS

后端 未结 3 1731
攒了一身酷
攒了一身酷 2021-01-12 09:40

I am building an app using swift and in that app there is one field for accepting expiry date for Debit card for payment purpose. How can I accept only month and year by usi

3条回答
  •  渐次进展
    2021-01-12 10:09

    I suggest you use this link: https://github.com/bendodson/MonthYearPickerView-Swift

    It is saving my time to validate valid dates months

     let expiryDatePicker = MonthYearPickerView()
    
    private func pickADate() {
        self.invitationTypeTemp = self.expiredDetailOutlet.text ?? "unknown error"
    
        //FORMAT DATE
       //        expiryDatePicker.datePickerMode = .date
    
        expiryDatePicker.backgroundColor = UIColor.white
        expiryDatePicker.setValue(UIColor.black, forKey: "textColor")
        expiryDatePicker.autoresizingMask = .flexibleWidth
        expiryDatePicker.contentMode = .center
        expiryDatePicker.frame = CGRect.init(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 300)
        self.view.addSubview(expiryDatePicker)
    
        toolBar = UIToolbar.init(frame: CGRect.init(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 50))
        toolBar.barStyle = .default
        toolBar.isTranslucent = true
        let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(onDoneButtonTappedDateExpired))
        let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(onCancelButtonTappedDateExpired))
    
        toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
        toolBar.isUserInteractionEnabled = true
        self.view.addSubview(toolBar)
    
        //DISABLE RIGHT ITEM & LEFT ITEM
       //        disableCancelAndSaveItems()
    
        //DISABLED SELECTION FOR ALL CELLS
       //        ableToSelectCellsAndButtons(isAble: false)
    
        //DISABLE RIGHT ITEM & LEFT ITEM
        isEnableCancelAndSaveItems(isEnabled: false)
    
        //SHOW GREY BACK GROUND
        showGreyOutView(isShow: true)
        expiryDatePicker.onDateSelected = { (month: Int, year: Int) in
            let string = String(format: "%02d/%d", month, year)
            self.expiredDetailOutlet.text = string
            NSLog(string) // should show something like 05/2015
        }
    }
    

提交回复
热议问题