Skip to content

Instantly share code, notes, and snippets.

@frosty
Created September 20, 2019 15:08
Show Gist options
  • Save frosty/6c49fe3836503dc29492c92c11f4fd4a to your computer and use it in GitHub Desktop.
Save frosty/6c49fe3836503dc29492c92c11f4fd4a to your computer and use it in GitHub Desktop.
diff --git a/WordPress/Classes/ViewRelated/Notifications/Controllers/NotificationsViewController.swift b/WordPress/Classes/ViewRelated/Notifications/Controllers/NotificationsViewController.swift
index b1349b9cf4..0e0f233fba 100644
--- a/WordPress/Classes/ViewRelated/Notifications/Controllers/NotificationsViewController.swift
+++ b/WordPress/Classes/ViewRelated/Notifications/Controllers/NotificationsViewController.swift
@@ -115,8 +115,8 @@ class NotificationsViewController: UITableViewController, UIViewControllerRestor
setupNavigationBar()
setupTableView()
- setupTableHeaderView()
setupTableFooterView()
+ layoutHeaderIfNeeded()
setupConstraints()
setupTableHandler()
setupRefreshControl()
@@ -124,6 +124,7 @@ class NotificationsViewController: UITableViewController, UIViewControllerRestor
setupFilterBar()
reloadTableViewPreservingSelection()
+
}
override func viewWillAppear(_ animated: Bool) {
@@ -185,18 +186,31 @@ class NotificationsViewController: UITableViewController, UIViewControllerRestor
tableViewHandler.updateRowAnimation = .none
}
- override func viewWillLayoutSubviews() {
- // in iOS 13, traitCollectionDidChange gets called when the view is created as oppose to
- // previous versions where it was called when added to the view hierarchy
- // which created this bug: https://github.com/wordpress-mobile/WordPress-iOS/issues/12495.
- // Apple suggests to perform any work involving traits in one of the layout methods
- // and then rely on traitCollectionDidChange for any future size class changes.
- if layoutForTheFirstTIme {
- layoutForTheFirstTIme = false
- setupTableHeaderView()
- setupNoResultsView()
- }
- }
+ override func viewDidLayoutSubviews() {
+ super.viewDidLayoutSubviews()
+
+ layoutHeaderIfNeeded()
+ }
+
+ private func layoutHeaderIfNeeded() {
+ precondition(tableHeaderView != nil)
+
+ // Fix: Update the Frame manually: Autolayout doesn't really help us, when it comes to Table Headers
+ let requiredSize = tableHeaderView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
+ var headerFrame = tableHeaderView.frame
+
+ if headerFrame.height != requiredSize.height {
+ headerFrame.size.height = requiredSize.height
+ tableHeaderView.frame = headerFrame
+ adjustNoResultsViewSize()
+
+ tableHeaderView.layoutIfNeeded()
+
+ // We reassign the tableHeaderView to force the UI to refresh. Yes, really.
+ tableView.tableHeaderView = tableHeaderView
+ tableView.setNeedsLayout()
+ }
+ }
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
@@ -204,10 +218,7 @@ class NotificationsViewController: UITableViewController, UIViewControllerRestor
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
- // table header views are a special kind of broken. This dispatch forces the table header to get a new layout
- // on the next redraw tick, which seems to be required.
DispatchQueue.main.async {
- self.setupTableHeaderView()
self.showNoResultsViewIfNeeded()
}
@@ -491,23 +502,6 @@ private extension NotificationsViewController {
WPStyleGuide.configureColors(view: view, tableView: tableView)
}
- func setupTableHeaderView() {
- precondition(tableHeaderView != nil)
-
- // Fix: Update the Frame manually: Autolayout doesn't really help us, when it comes to Table Headers
- let requiredSize = tableHeaderView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
- var headerFrame = tableHeaderView.frame
- headerFrame.size.height = requiredSize.height
- tableHeaderView.frame = headerFrame
- adjustNoResultsViewSize()
-
- tableHeaderView.layoutIfNeeded()
-
- // We reassign the tableHeaderView to force the UI to refresh. Yes, really.
- tableView.tableHeaderView = tableHeaderView
- tableView.setNeedsLayout()
- }
-
func setupTableFooterView() {
// Fix: Hide the cellSeparators, when the table is empty
tableView.tableFooterView = UIView()
@@ -534,6 +528,8 @@ private extension NotificationsViewController {
setupAppRatings()
showInlinePrompt()
}
+
+ layoutHeaderIfNeeded()
}
func setupRefreshControl() {
@@ -1284,7 +1280,7 @@ internal extension NotificationsViewController {
self.inlinePromptSpaceConstraint.isActive = true
UIView.animate(withDuration: WPAnimationDurationDefault, delay: InlinePrompt.animationDelay, options: .curveEaseIn, animations: {
self.inlinePromptView.alpha = WPAlphaFull
- self.setupTableHeaderView()
+ self.layoutHeaderIfNeeded()
})
WPAnalytics.track(.appReviewsSawPrompt)
@@ -1296,7 +1292,7 @@ internal extension NotificationsViewController {
delay: delay,
animations: {
self.inlinePromptView.alpha = WPAlphaZero
- self.setupTableHeaderView()
+ self.layoutHeaderIfNeeded()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment