Skip to content

Commit

Permalink
Fix layout on landscape/resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
hughbe committed Feb 12, 2018
1 parent edd392d commit d971d47
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion DayDatePicker.podspec
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 15 additions & 8 deletions DayDatePicker/Classes/DayDatePickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
}
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -301,7 +308,7 @@ extension DayDatePickerView : UITableViewDataSource, UITableViewDelegate {

alignTableViewToRow(tableView: tableView)
}

public func scrollViewDidScrollToTop(_ scrollView: UIScrollView) {
guard let tableView = scrollView as? UITableView else {
return
Expand Down
21 changes: 14 additions & 7 deletions DayDatePicker/Classes/TimePickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit d971d47

Please sign in to comment.