Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDatePicker on button tap

So far I've only found date-picker bound to UITextField, but I need a date-picker which opens on UIButton tap in the same way as it does with the UITextField.

Is it possible? And if so, how? Thanks in advance.

like image 707
giuse Avatar asked Oct 19 '25 03:10

giuse


1 Answers

@IBAction func BtnClicked(sender: AnyObject) {
    var picker : UIDatePicker = UIDatePicker()
    picker.datePickerMode = UIDatePickerMode.Date
    picker.addTarget(self, action: "dueDateChanged:", forControlEvents: UIControlEvents.ValueChanged)
    var pickerSize : CGSize = picker.sizeThatFits(CGSizeZero)
    picker.frame = CGRectMake(0.0, 250, pickerSize.width, 460)
    // you probably don't want to set background color as black
    // picker.backgroundColor = UIColor.blackColor()
    self.view.addSubview(picker)
}

func dueDateChanged(sender:UIDatePicker){
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
    dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
    self.myLabel.text = dateFormatter.stringFromDate(dueDatePickerView.date)
}
like image 59
iAnurag Avatar answered Oct 21 '25 19:10

iAnurag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!