DatePicker, TimePicker

2024. 5. 22. 17:33Workout

 

  • 시작일시, 종료일시  각각의 TextField Form이  있을것
  • 터치시  datePicker or timePicker 가 keyboard 대신 올라올것
    • 더보기
      IiOS 13.4 부터 UIPickerView 스타일이 다양 해짐

      @available(iOS 13.4, *)

      public enum UIDatePickerStyle : Int, @unchecked Sendable {

       

          

          /// Automatically pick the best style available for the current platform & mode.

          case automatic = 0

       

          /// Use the wheels (UIPickerView) style. Editing occurs inline.

          case wheels = 1 // 휠밖에 모르던시절 

       

          /// Use a compact style for the date picker. Editing occurs in an overlay.

          case compact = 2 // 원하는 날짜 선택하는 맛이 없음

       

          /// Use a style for the date picker that allows editing in place.

          @available(iOS 14.0, *)

          case inline = 3 // 원하는 날짜를 선택하고 싶어서 차용

      }

 @IBOutlet weak var datetextFiled: UITextFiled!
 @IBOutlet weak var timetextFiled: UITextFiled!
 
func viewDidLoad() {
	super.viewDidLoad()
    datetextFiled.datePicker(target: self,
                                 doneAction: #selector(doneAction_1),
                                 cancelAction: #selector(cancelAction_1),
                                 datePickerMode: .date)
            
    timetextFiled.datePicker(target: self,
                         doneAction: #selector(doneAction_2),
                         cancelAction: #selector(cancelAction_2),
                         datePickerMode: .time)
}

.... 
extention UIViewcontroller {

    func datePicker<T>(target: T,
                       doneAction: Selector,
                       cancelAction: Selector,
                       datePickerMode: UIDatePicker.Mode = .date) {
        
        guard self.inputView == nil else {
            return
        }
        // Code goes here
        let screenWidth = UIScreen.main.bounds.width
        
        func buttonItem(withSystemItemStyle style: UIBarButtonItem.SystemItem, title:String = "") -> UIBarButtonItem {
            let buttonTarget = style == .flexibleSpace ? nil : target
            let action: Selector? = {
                switch style {
                case .cancel:
                    return cancelAction
                case .done:
                    return doneAction
                default:
                    return nil
                }
            }()
            let barButtonItem : UIBarButtonItem
            
            if title.count > 0 {
                barButtonItem = UIBarButtonItem(title: title, style: .plain, target: buttonTarget, action: action)
            }else{
                barButtonItem = UIBarButtonItem(barButtonSystemItem: style,
                                                target: buttonTarget,
                                                action: action)
            }
            return barButtonItem
        }
        
        let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 216))
        datePicker.datePickerMode = datePickerMode
        if #available(iOS 14, *) {
            datePicker.preferredDatePickerStyle =  datePickerMode == .date ? .inline : .wheels
        }
        
        datePicker.locale =  getDeviceLocale
        if datePickerMode == .date {
            datePicker.timeZone = .autoupdatingCurrent
            datePicker.minimumDate = Date()
        }
        self.inputView = datePicker
        
        let toolBar = UIToolbar(frame: CGRect(x: 0,
                                              y: 0,
                                              width: screenWidth,
                                              height: 44))
        toolBar.setItems([buttonItem(withSystemItemStyle: .cancel, title:"Cancel".localized),
                          buttonItem(withSystemItemStyle: .flexibleSpace),
                          buttonItem(withSystemItemStyle: .done, title:"OK".localized)],
                         animated: true)
        self.inputAccessoryView = toolBar
    }
    
}