在Swift中使用24小时DatePicker

时间:2014-07-09 11:56:12

标签: swift uidatepicker

我很难将日期选择器设置为只允许Swift中的时间和24小时格式,任何想法如何做到这一点?

由于

3 个答案:

答案 0 :(得分:7)

OP说他正试图在Swift中重新创建this answer。这个答案是一个kludge(它取决于使用24小时格式的丹麦语语言环境,而不是实际解决问题),但这里有你如何在Swift中那个

var datePicker = UIDatePicker() // Although you probably have an IBOutlet
datePicker.datePickerMode = UIDatePickerMode.Time
datePicker.locale = NSLocale(localeIdentifier: "da_DK")

但这是一个kludge - 滚动你自己的UIPicker

<强>更新 OP说你自己滚动是麻烦&#34;麻烦&#34; - 它实际上非常简单 - 这是一种简单的方法(虽然更好的方法是将UIPickerView子类化)...

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var myDatePicker: UIPickerView!
    private var calendar = Calendar.autoupdatingCurrent

    override func viewDidLoad() {
        super.viewDidLoad()
        self.time = Date() // updates the picker to "now"
    }

    // MARK: UIPickerViewDataSource
    func numberOfComponents(in: UIPickerView) -> Int { return 2 }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 { // Hours
            return 24
        }
        return 12 // five minute intervals
    }

    // MARK: UIPickerViewDelegate
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 { // Hours
            return String(format: "%02d", row)
        }
        return String(format: "%02d", row * 5)
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        print("picker changed, selected \(self.time)")
    }

    // MARK: Getting & setting the time
    var time: Date {
        get {
            let unitFlags: Calendar.Unit = [.year, .day, .hour, .minute, .second]
            var components = self.calendar.components(unitFlags, from: Date())
            components.hour = self.myDatePicker.selectedRow(inComponent: 0)
            components.minute = self.myDatePicker.selectedRow(inComponent: 1) * 5
            components.second = 0
            if let date = self.calendar.date(from: components) {
                return date
            }
            return Date() // shouldn't get here
        }
        set {
            var components = self.calendar.components([.hour, .minute], from: newValue)
            if let hour = components.hour, let minute = components.minute {
                self.myDatePicker.selectRow(hour, inComponent: 0, animated: true)
                self.myDatePicker.selectRow(minute / 5, inComponent: 1, animated: true)
            }
        }
    }
}

答案 1 :(得分:0)

IOS Swift4

    let datePickerView: UIDatePicker = UIDatePicker()
    // For 12 hours Format
    datePickerView.locale = Locale(identifier: "en_US")
    // For 24 Hrs
    datePickerView.locale = Locale(identifier: "en_GB")

答案 2 :(得分:0)

这就是您使用上述相同技巧在视图中执行相同操作的方式。

import SwiftUI

struct ContentView: View {
    @State var time: Date = Date()
    var body: some View {
        VStack {
            DatePicker("Start Time", selection: $time, displayedComponents: .hourAndMinute)
                .labelsHidden()
                //Trick the Hour picker to show 24-hr format
                .environment(\.locale, Locale(identifier: "en_DK"))
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}