https://github.com/potato512/SYSwiftLearning
效果圖
範例程式碼
// 執行個體化UIDatePicker(預設寬高分別是:螢幕的寬度,216的高度)let datepicker = UIDatePicker()self.view.addSubview(datepicker) // 屬性設定datepicker.backgroundColor = UIColor.lightGrayColor() // 日期顯示內容datepicker.datePickerMode = UIDatePickerMode.DateAndTime// 設定預設時間datepicker.setDate(NSDate(), animated: true) // 最小、最大時間選擇設定datepicker.minimumDate = NSDate(timeInterval: -93312000, sinceDate: NSDate())datepicker.maximumDate = NSDate(timeIntervalSinceNow: 93312000) // 時間分鐘間隔設定datepicker.minuteInterval = 5 // 將日期選取器地區設定為中文(預設顯示英文)datepicker.locale = NSLocale(localeIdentifier: "zh_CN") // 添加方法datepicker.addTarget(self, action:Selector("dateChanged:"), forControlEvents: UIControlEvents.ValueChanged)
// 顯示選擇時間let label = UILabel(frame: CGRectMake(10.0, (CGRectGetHeight(self.view.bounds) - 10.0 - 40.0), (CGRectGetWidth(self.view.bounds) - 10.0 * 2), 40.0))self.view.addSubview(label)label.autoresizingMask = UIViewAutoresizing.FlexibleTopMarginlabel.backgroundColor = UIColor.yellowColor()label.textColor = UIColor.redColor()label.tag = 1000
// MARK: - 日期選取器回應程式法func dateChanged(datePicker:UIDatePicker){ // 更新提醒時間文字框 let formatter = NSDateFormatter() formatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss" let date = datePicker.date let dateText = formatter.stringFromDate(date) let label = self.view.viewWithTag(1000) as! UILabel label.text = dateText}