前端之家收集整理的这篇文章主要介绍了
Swift UIDatePikcer,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import UIKit
class ViewController: UIViewController {
var label:UILabel!
var datePikcer:UIDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
label = UILabel.init(frame: CGRectMake(10,200,self.view.frame.size.width-20,20))
label.textAlignment = NSTextAlignment.Center
label.text = "显示时间"
self.view.addSubview(label)
datePikcer = UIDatePicker.init(frame: CGRectMake(10,64,self.view.frame.width-20,100))
datePikcer.backgroundColor = UIColor.orangeColor()
datePikcer.datePickerMode = UIDatePickerMode.Date //设置显示的样式
datePikcer.date = NSDate() //设置默认时间
datePikcer.addTarget(self,action:Selector("datePickerValueChange:"),forControlEvents: UIControlEvents.ValueChanged)
self.view.addSubview(datePikcer)
}
func datePickerValueChange(sender :UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
label.text = dateFormatter.stringFromDate(datePikcer.date)
}
}