Skip to content

Commit

Permalink
Merge pull request #11 from NYPL-Simplified/header_view_#4
Browse files Browse the repository at this point in the history
Padding for header views (fixed with dynamic section headers using autolayout)..
  • Loading branch information
Aferdita authored Mar 30, 2017
2 parents 4192609 + 233febf commit 5c2b692
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 41 deletions.
31 changes: 18 additions & 13 deletions NYPLCardCreator/Classes/ConfirmValidAddressViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ final class ConfirmValidAddressViewController: TableViewController {
}

self.tableView.allowsSelection = false
self.tableView.tableHeaderView = self.headerLabel

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

override func viewDidLayoutSubviews() {
let origin_x = self.tableView.tableHeaderView!.frame.origin.x
let origin_y = self.tableView.tableHeaderView!.frame.origin.y
let size = self.tableView.tableHeaderView!.sizeThatFits(CGSize(width: self.view.bounds.width, height: CGFloat.greatestFiniteMagnitude))

let adjustedWidth = (size.width > CGFloat(375)) ? CGFloat(375.0) : size.width
let padding = CGFloat(30.0)
self.headerLabel.frame = CGRect(x: origin_x, y: origin_y, width: adjustedWidth, height: size.height + padding)

self.tableView.tableHeaderView = self.headerLabel
}

// MARK: UITableViewDataSource

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

func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return 44
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let containerView = UIView()
containerView.addSubview(self.headerLabel)
self.headerLabel.autoPinEdge(toSuperviewMargin: .left)
self.headerLabel.autoPinEdge(toSuperviewMargin: .right)
self.headerLabel.autoPinEdge(toSuperviewEdge: .top, withInset: 20)
self.headerLabel.autoPinEdge(toSuperviewEdge: .bottom, withInset: 20)
return containerView
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let addressCell = tableView.dequeueReusableCell(
withIdentifier: ConfirmValidAddressViewController.addressCellReuseIdentifier,
Expand Down
57 changes: 43 additions & 14 deletions NYPLCardCreator/Classes/UserCredentialsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import UIKit
final class UserCredentialsViewController: TableViewController {
fileprivate var cells: [UITableViewCell]
fileprivate let headerLabel: UILabel
fileprivate var activityView: ActivityTitleView
fileprivate let cardType: CardType

fileprivate let configuration: CardCreatorConfiguration
Expand Down Expand Up @@ -34,6 +35,11 @@ final class UserCredentialsViewController: TableViewController {
self.cardType = cardType

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

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

NotificationCenter.default.addObserver(
self,
selector: #selector(UserCredentialsViewController.signInFinished),
name: NSNotification.Name(rawValue: "NYPLSettingsAccountsSignInFinishedNotification"),
object: nil)

self.prepareTableViewCells()
}

Expand Down Expand Up @@ -102,31 +114,23 @@ final class UserCredentialsViewController: TableViewController {

self.tableView.estimatedRowHeight = 120
self.tableView.allowsSelection = false
self.tableView.tableHeaderView = headerLabel
}

override func viewDidLayoutSubviews() {
let origin_x = self.tableView.tableHeaderView!.frame.origin.x
let origin_y = self.tableView.tableHeaderView!.frame.origin.y
let size = self.tableView.tableHeaderView!.sizeThatFits(CGSize(width: self.view.bounds.width, height: CGFloat.greatestFiniteMagnitude))

let adjustedWidth = (size.width > CGFloat(375)) ? CGFloat(375.0) : size.width
let padding = CGFloat(30.0)
self.headerLabel.frame = CGRect(x: origin_x, y: origin_y, width: adjustedWidth, height: size.height + padding)

self.tableView.tableHeaderView = self.headerLabel
}

override func viewDidAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.configuration.completionHandler(self.username, self.pin, false)
self.activityView.isHidden = false
}

fileprivate func prepareTableViewCells() {
for cell in self.cells {
cell.backgroundColor = UIColor.clear
}
}

@objc fileprivate func signInFinished() {
self.navigationItem.titleView = nil
}

// MARK: UITableViewDataSource

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

func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 44
} else {
return 0
}
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0
if section == 0 {
return UITableViewAutomaticDimension
} else {
return 0
}
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let containerView = UIView()
containerView.addSubview(self.headerLabel)
containerView.addSubview(self.activityView)
self.activityView.autoAlignAxis(toSuperviewAxis: .vertical)
self.activityView.autoPinEdge(toSuperviewEdge: .top, withInset: 16)
self.headerLabel.autoPinEdge(toSuperviewMargin: .left)
self.headerLabel.autoPinEdge(toSuperviewMargin: .right)
self.headerLabel.autoPinEdge(.top, to: .bottom, of: self.activityView, withOffset: 16)
self.headerLabel.autoPinEdge(toSuperviewEdge: .bottom, withInset: 20)
return containerView
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
Expand Down
41 changes: 27 additions & 14 deletions NYPLCardCreator/Classes/UserSummaryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,6 @@ final class UserSummaryViewController: TableViewController {

self.tableView.estimatedRowHeight = 120
self.tableView.allowsSelection = false
self.tableView.tableHeaderView = headerLabel
}

override func viewDidLayoutSubviews() {
let origin_x = self.tableView.tableHeaderView!.frame.origin.x
let origin_y = self.tableView.tableHeaderView!.frame.origin.y
let size = self.tableView.tableHeaderView!.sizeThatFits(CGSize(width: self.view.bounds.width, height: CGFloat.greatestFiniteMagnitude))

let adjustedWidth = (size.width > CGFloat(375)) ? CGFloat(375.0) : size.width
let padding = CGFloat(30.0)
self.headerLabel.frame = CGRect(x: origin_x, y: origin_y, width: adjustedWidth, height: size.height + padding)

self.tableView.tableHeaderView = self.headerLabel
}

fileprivate func prepareTableViewCells() {
Expand Down Expand Up @@ -159,7 +146,33 @@ final class UserSummaryViewController: TableViewController {
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0
if section == 0 {
return UITableViewAutomaticDimension
} else {
return 0
}
}

func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 40
} else {
return 0
}
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
let containerView = UIView()
containerView.addSubview(self.headerLabel)
self.headerLabel.autoPinEdge(toSuperviewMargin: .left)
self.headerLabel.autoPinEdge(toSuperviewMargin: .right)
self.headerLabel.autoPinEdge(toSuperviewEdge: .top, withInset: 20)
self.headerLabel.autoPinEdge(toSuperviewEdge: .bottom, withInset: 20)
return containerView
} else {
return nil
}
}

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

0 comments on commit 5c2b692

Please sign in to comment.