The use of Uidatepicker in Swift __swift learning

Source: Internet
Author: User

Https://github.com/potato512/SYSwiftLearning

Effect chart



Sample code

Instantiate Uidatepicker (default width is: screen width, height of 216) let
DatePicker = Uidatepicker ()
Self.view.addSubview (DatePicker)
        
//property settings
Datepicker.backgroundcolor = Uicolor.lightgraycolor ()
        
//Date Display Properties
Datepicker.datepickermode = Uidatepickermode.dateandtime

//Set default time
Datepicker.setdate (NSDate (), animated:true)
        
//min, max time selection settings
datepicker.minimumdate = NSDate (timeinterval: -93312000, Sincedate:nsdate ())
datepicker.maximumdate = NSDate (timeintervalsincenow:93312000)
        
//Time minute interval set
Datepicker.minuteinterval = 5
        
// Set Date selector area to Chinese (default display english)
Datepicker.locale = Nslocale (localeidentifier: "ZH_CN")
        
//Add Method
Datepicker.addtarget (Self, Action:selector ("datechanged:"), forControlEvents:UIControlEvents.ValueChanged)
Display Select time let
label = Uilabel (Frame:cgrectmake (10.0, Cgrectgetheight (self.view.bounds)-10.0-40.0), (Cgrectgetwi DTH (Self.view.bounds)-10.0 * 2), 40.0)
self.view.addSubview (label)
Label.autoresizingmask = Uiviewautoresizing.flexibletopmargin
Label.backgroundcolor = Uicolor.yellowcolor ()
Label.textColor = Uicolor.redcolor ()
Label.tag = 1000
MARK:-Date Selector response method
func datechanged (datepicker:uidatepicker)
{
        //Update reminder time text box let
        formatter = NSDateFormatter ()
        Formatter.dateformat = "yyyy year mm month DD Day HH:MM:SS" let
        date = Datepicker.date
        let Datetext = formatter.stringfromdate (date) let

        label = SELF.VIEW.VIEWWITHTAG (1000) as! Uilabel
        label.text = Datetext
}






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.