diff --git a/DayDatePicker.podspec b/DayDatePicker.podspec index 388069f..fb7a97f 100644 --- a/DayDatePicker.podspec +++ b/DayDatePicker.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'DayDatePicker' - s.version = '1.4' + s.version = '1.5' s.summary = 'A custom and customizable UIDatePicker which displays the day of the week alongside the day column' s.description = <<-DESC diff --git a/DayDatePicker/Classes/DayDatePickerView.swift b/DayDatePicker/Classes/DayDatePickerView.swift index c502d18..2f2ebad 100644 --- a/DayDatePicker/Classes/DayDatePickerView.swift +++ b/DayDatePicker/Classes/DayDatePickerView.swift @@ -47,7 +47,7 @@ public class DayDatePickerView : UIControl { let date = Date(year: year, month: month, day: day) setDate(date: date, animated: animated) } - + public func setDate(date: Foundation.Date, animated: Bool) { let dateDate = Date(date: date) setDate(date: dateDate, animated: animated) @@ -62,7 +62,7 @@ public class DayDatePickerView : UIControl { let reloadMonthTableView = date.year != _date.year let reloadDayTableView = reloadMonthTableView || date.month != _date.month _date = date - + if reloadMonthTableView { monthTableView.reloadAndLayout() } @@ -83,7 +83,7 @@ public class DayDatePickerView : UIControl { let minDate = Date(year: year, month: month, day: day) setMinDate(minDate: minDate, animated: animated) } - + public func setMinDate(minDate: Foundation.Date, animated: Bool) { let minDateDate = Date(date: minDate) setMinDate(minDate: minDateDate, animated: animated) @@ -186,9 +186,6 @@ extension DayDatePickerView { dayRange = Calendar.current.range(of: .day, in: .month, for: _date.date) monthRange = Calendar.current.range(of: .month, in: .year, for: _date.date) yearRange = Calendar.current.range(of: .year, in: .era, for: _date.date) - - superview?.layoutIfNeeded() - setDate(date: _date, animated: false) } private func setupTableView(tableView: UITableView) { @@ -197,7 +194,6 @@ extension DayDatePickerView { tableView.showsVerticalScrollIndicator = false tableView.separatorStyle = .none tableView.backgroundColor = UIColor.white - tableView.contentInset = UIEdgeInsets(top: (frame.size.height - rowHeight) / 2, left: 0, bottom: (frame.size.height - rowHeight) / 2, right: 0) tableView.delegate = self tableView.dataSource = self @@ -206,6 +202,17 @@ extension DayDatePickerView { addSubview(tableView) } + + public override func layoutSubviews() { + super.layoutSubviews() + + let contentInset = UIEdgeInsets(top: (frame.size.height - rowHeight) / 2, left: 0, bottom: (frame.size.height - rowHeight) / 2, right: 0) + dayTableView.contentInset = contentInset + monthTableView.contentInset = contentInset + yearTableView.contentInset = contentInset + + setDate(date: _date, animated: false) + } } // Table view data. @@ -301,7 +308,7 @@ extension DayDatePickerView : UITableViewDataSource, UITableViewDelegate { alignTableViewToRow(tableView: tableView) } - + public func scrollViewDidScrollToTop(_ scrollView: UIScrollView) { guard let tableView = scrollView as? UITableView else { return diff --git a/DayDatePicker/Classes/TimePickerView.swift b/DayDatePicker/Classes/TimePickerView.swift index 7e29eb8..69b0aa4 100644 --- a/DayDatePicker/Classes/TimePickerView.swift +++ b/DayDatePicker/Classes/TimePickerView.swift @@ -51,7 +51,7 @@ public class TimePickerView : UIControl { time.minute = time.minute.round(toNearest: minuteInterval) let reloadMinuteTableView = time.hour != _time.hour _time = time - + if reloadMinuteTableView { minuteTableView.reloadAndLayout() } @@ -150,9 +150,6 @@ extension TimePickerView { hourRange = Calendar.current.range(of: .hour, in: .day, for: Date()) minuteRange = Calendar.current.range(of: .minute, in: .hour, for: Date()) - - superview?.layoutIfNeeded() - setTime(time: _time, animated: false) } private func setupTableView(tableView: UITableView) { @@ -166,11 +163,21 @@ extension TimePickerView { tableView.delegate = self tableView.dataSource = self - + tableView.scrollsToTop = false addSubview(tableView) } + + public override func layoutSubviews() { + super.layoutSubviews() + + let contentInset = UIEdgeInsets(top: (frame.size.height - rowHeight) / 2, left: 0, bottom: (frame.size.height - rowHeight) / 2, right: 0) + hourTableView.contentInset = contentInset + minuteTableView.contentInset = contentInset + + setTime(time: _time, animated: false) + } } // Table view data. @@ -227,11 +234,11 @@ extension TimePickerView : UITableViewDataSource, UITableViewDelegate { return cell; } - + public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return rowHeight } - + public func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return rowHeight }