swift之Mac中NSDatePicker

Date Picker控件的主要UI属性说明如下。

Style: 3种U样式 Textual、 Textual With Stepper.和 Graphical分别表示文本日历、自增

减的文本日历和图形化显示的日历。

Selections: 鼠标点击选择的模式, Date Range和 Single Range分别表示是整个日期范围

还是单个部分的选择。

Elements:日期按年月日时分秒多种组合显示,可以分段控制,如不显示年月日只显示

时分秒

Date:日期的初始值

Display: Background和 Border分别控制是否显示背景和边框

Text Color:文字的颜色,仅对 Textual和 Textual With Stepper两种U样式的 Date Picker

Background:背景的颜色,仅对 Graphical样式的 Date Picker有效。

有效

获取日期

日期选择器控件的 date Value和 timeInterval属性代表不同数据类型的日期,属性定义如下:

public var datevalue: Date

public var timeInterval: TimeInterval

fileprivate var datePicer: NSDatePicker = {

        let datePicer = NSDatePicker(frame: NSMakeRect(0, 0, 300, 300))

        datePicer.wantsLayer = true

        //设置当前日期

        let components = NSDateComponents()

        components.day = 5

        components.month = 01

        components.year = 2016

        components.hour = 19

        components.minute = 30

        let calendar = NSCalendar.current

        let newDate = calendar.date(from: components as DateComponents)

        datePicer.dateValue = newDate!

        //设置最小日期

        datePicer.minDate = newDate

        //设置最大日期

        let date = NSDate()

        datePicer.maxDate = date as Date

        datePicer.action = #selector(updateDateResult(datePicer:))

        

        return datePicer

    }()

    

    @objc func updateDateResult(datePicer:NSDatePicker) {

        //拿到当前日期

        let theDate = datePicer.dateValue

        

        //把日期转化为制定格式

        let formatter = DateFormatter()

        formatter.dateFormat = "yyyy-MM-dd"

        let dateString = formatter.string(from: theDate)

        NSLog("dataString-----(dateString)")

    }

原文地址:https://www.cnblogs.com/sundaymac/p/10338347.html