Skip to content

Commit 5c2b692

Browse files
author
Aferdita
authored
Merge pull request #11 from NYPL-Simplified/header_view_#4
Padding for header views (fixed with dynamic section headers using autolayout)..
2 parents 4192609 + 233febf commit 5c2b692

File tree

3 files changed

+88
-41
lines changed

3 files changed

+88
-41
lines changed

NYPLCardCreator/Classes/ConfirmValidAddressViewController.swift

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ final class ConfirmValidAddressViewController: TableViewController {
5858
}
5959

6060
self.tableView.allowsSelection = false
61-
self.tableView.tableHeaderView = self.headerLabel
6261

6362
self.navigationItem.rightBarButtonItem =
6463
UIBarButtonItem(title: NSLocalizedString(
@@ -69,18 +68,6 @@ final class ConfirmValidAddressViewController: TableViewController {
6968
action: #selector(addressConfirmed))
7069
}
7170

72-
override func viewDidLayoutSubviews() {
73-
let origin_x = self.tableView.tableHeaderView!.frame.origin.x
74-
let origin_y = self.tableView.tableHeaderView!.frame.origin.y
75-
let size = self.tableView.tableHeaderView!.sizeThatFits(CGSize(width: self.view.bounds.width, height: CGFloat.greatestFiniteMagnitude))
76-
77-
let adjustedWidth = (size.width > CGFloat(375)) ? CGFloat(375.0) : size.width
78-
let padding = CGFloat(30.0)
79-
self.headerLabel.frame = CGRect(x: origin_x, y: origin_y, width: adjustedWidth, height: size.height + padding)
80-
81-
self.tableView.tableHeaderView = self.headerLabel
82-
}
83-
8471
// MARK: UITableViewDataSource
8572

8673
func numberOfSectionsInTableView(_ tableView: UITableView) -> Int {
@@ -91,6 +78,24 @@ final class ConfirmValidAddressViewController: TableViewController {
9178
return 1
9279
}
9380

81+
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
82+
return 44
83+
}
84+
85+
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
86+
return UITableViewAutomaticDimension
87+
}
88+
89+
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
90+
let containerView = UIView()
91+
containerView.addSubview(self.headerLabel)
92+
self.headerLabel.autoPinEdge(toSuperviewMargin: .left)
93+
self.headerLabel.autoPinEdge(toSuperviewMargin: .right)
94+
self.headerLabel.autoPinEdge(toSuperviewEdge: .top, withInset: 20)
95+
self.headerLabel.autoPinEdge(toSuperviewEdge: .bottom, withInset: 20)
96+
return containerView
97+
}
98+
9499
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
95100
let addressCell = tableView.dequeueReusableCell(
96101
withIdentifier: ConfirmValidAddressViewController.addressCellReuseIdentifier,

NYPLCardCreator/Classes/UserCredentialsViewController.swift

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import UIKit
55
final class UserCredentialsViewController: TableViewController {
66
fileprivate var cells: [UITableViewCell]
77
fileprivate let headerLabel: UILabel
8+
fileprivate var activityView: ActivityTitleView
89
fileprivate let cardType: CardType
910

1011
fileprivate let configuration: CardCreatorConfiguration
@@ -34,6 +35,11 @@ final class UserCredentialsViewController: TableViewController {
3435
self.cardType = cardType
3536

3637
self.headerLabel = UILabel()
38+
self.activityView = ActivityTitleView(title:
39+
NSLocalizedString(
40+
"Signing In...",
41+
comment: "A title telling the user that the app is busy signing in with the new account that was just created."))
42+
self.activityView.isHidden = true
3743

3844
self.usernameCell = SummaryCell(section: NSLocalizedString("Username", comment: "Title of the section for the user's chosen username"),
3945
cellText: self.username)
@@ -62,6 +68,12 @@ final class UserCredentialsViewController: TableViewController {
6268
target: self,
6369
action: #selector(openCatalog))
6470

71+
NotificationCenter.default.addObserver(
72+
self,
73+
selector: #selector(UserCredentialsViewController.signInFinished),
74+
name: NSNotification.Name(rawValue: "NYPLSettingsAccountsSignInFinishedNotification"),
75+
object: nil)
76+
6577
self.prepareTableViewCells()
6678
}
6779

@@ -102,31 +114,23 @@ final class UserCredentialsViewController: TableViewController {
102114

103115
self.tableView.estimatedRowHeight = 120
104116
self.tableView.allowsSelection = false
105-
self.tableView.tableHeaderView = headerLabel
106-
}
107-
108-
override func viewDidLayoutSubviews() {
109-
let origin_x = self.tableView.tableHeaderView!.frame.origin.x
110-
let origin_y = self.tableView.tableHeaderView!.frame.origin.y
111-
let size = self.tableView.tableHeaderView!.sizeThatFits(CGSize(width: self.view.bounds.width, height: CGFloat.greatestFiniteMagnitude))
112-
113-
let adjustedWidth = (size.width > CGFloat(375)) ? CGFloat(375.0) : size.width
114-
let padding = CGFloat(30.0)
115-
self.headerLabel.frame = CGRect(x: origin_x, y: origin_y, width: adjustedWidth, height: size.height + padding)
116-
117-
self.tableView.tableHeaderView = self.headerLabel
118117
}
119118

120119
override func viewDidAppear(_ animated: Bool) {
121120
super.viewWillAppear(animated)
122121
self.configuration.completionHandler(self.username, self.pin, false)
122+
self.activityView.isHidden = false
123123
}
124124

125125
fileprivate func prepareTableViewCells() {
126126
for cell in self.cells {
127127
cell.backgroundColor = UIColor.clear
128128
}
129129
}
130+
131+
@objc fileprivate func signInFinished() {
132+
self.navigationItem.titleView = nil
133+
}
130134

131135
// MARK: UITableViewDataSource
132136

@@ -142,8 +146,33 @@ final class UserCredentialsViewController: TableViewController {
142146
return self.cells[indexPath.section]
143147
}
144148

149+
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
150+
if section == 0 {
151+
return 44
152+
} else {
153+
return 0
154+
}
155+
}
156+
145157
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
146-
return 0
158+
if section == 0 {
159+
return UITableViewAutomaticDimension
160+
} else {
161+
return 0
162+
}
163+
}
164+
165+
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
166+
let containerView = UIView()
167+
containerView.addSubview(self.headerLabel)
168+
containerView.addSubview(self.activityView)
169+
self.activityView.autoAlignAxis(toSuperviewAxis: .vertical)
170+
self.activityView.autoPinEdge(toSuperviewEdge: .top, withInset: 16)
171+
self.headerLabel.autoPinEdge(toSuperviewMargin: .left)
172+
self.headerLabel.autoPinEdge(toSuperviewMargin: .right)
173+
self.headerLabel.autoPinEdge(.top, to: .bottom, of: self.activityView, withOffset: 16)
174+
self.headerLabel.autoPinEdge(toSuperviewEdge: .bottom, withInset: 20)
175+
return containerView
147176
}
148177

149178
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

NYPLCardCreator/Classes/UserSummaryViewController.swift

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,6 @@ final class UserSummaryViewController: TableViewController {
117117

118118
self.tableView.estimatedRowHeight = 120
119119
self.tableView.allowsSelection = false
120-
self.tableView.tableHeaderView = headerLabel
121-
}
122-
123-
override func viewDidLayoutSubviews() {
124-
let origin_x = self.tableView.tableHeaderView!.frame.origin.x
125-
let origin_y = self.tableView.tableHeaderView!.frame.origin.y
126-
let size = self.tableView.tableHeaderView!.sizeThatFits(CGSize(width: self.view.bounds.width, height: CGFloat.greatestFiniteMagnitude))
127-
128-
let adjustedWidth = (size.width > CGFloat(375)) ? CGFloat(375.0) : size.width
129-
let padding = CGFloat(30.0)
130-
self.headerLabel.frame = CGRect(x: origin_x, y: origin_y, width: adjustedWidth, height: size.height + padding)
131-
132-
self.tableView.tableHeaderView = self.headerLabel
133120
}
134121

135122
fileprivate func prepareTableViewCells() {
@@ -159,7 +146,33 @@ final class UserSummaryViewController: TableViewController {
159146
}
160147

161148
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
162-
return 0
149+
if section == 0 {
150+
return UITableViewAutomaticDimension
151+
} else {
152+
return 0
153+
}
154+
}
155+
156+
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
157+
if section == 0 {
158+
return 40
159+
} else {
160+
return 0
161+
}
162+
}
163+
164+
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
165+
if section == 0 {
166+
let containerView = UIView()
167+
containerView.addSubview(self.headerLabel)
168+
self.headerLabel.autoPinEdge(toSuperviewMargin: .left)
169+
self.headerLabel.autoPinEdge(toSuperviewMargin: .right)
170+
self.headerLabel.autoPinEdge(toSuperviewEdge: .top, withInset: 20)
171+
self.headerLabel.autoPinEdge(toSuperviewEdge: .bottom, withInset: 20)
172+
return containerView
173+
} else {
174+
return nil
175+
}
163176
}
164177

165178
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

0 commit comments

Comments
 (0)