From 5e161b3d2a3496e15a1601503c207d6d0de165fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maik=20A=C3=9Fhoff?= Date: Sun, 7 Jul 2024 13:37:17 +0200 Subject: [PATCH 1/5] Simplify customization of SPAppleTVButton's focus effect using background images for the normal and focused state. In addition, clear the background color of all subviews to only have the wanted background image and to fix the "unsharp" border caused by `_UIFloatingContentCornerRadiusAnimatingView`. --- .../Common/SPAppleTVButton.swift | 42 +++++++++---------- .../Common/SPNativeScreenViewController.swift | 10 +++-- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/Common/SPAppleTVButton.swift b/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/Common/SPAppleTVButton.swift index 944702e45..17ae85472 100644 --- a/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/Common/SPAppleTVButton.swift +++ b/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/Common/SPAppleTVButton.swift @@ -7,34 +7,30 @@ import UIKit -extension UIView { - var allSubviews: [UIView] { - subviews.reduce(into: [self]) { array, subview in - array += subview.allSubviews - } +/// A button that enforces clear background colors. +/// To style the background, use ``UIButton.setBackgroundImage(_ image:for:)``. +/// This suppresses unwanted background and border effects automatically added when using +/// the system button type while keeping the default appearance, drop shadow, and scaling effect. +final class SPAppleTVButton: UIButton { + override func layoutSubviews() { + super.layoutSubviews() + clearBackgroundColors() } } -class SPAppleTVButton: UIButton { - var viewBeforeUITitleView: UIView? { allSubviews.dropFirst(allSubviews.count - 2).first } +extension UIView { + + /// Sets the background colors of all the view's subviews to ``UIColor.clear``. + /// - Parameter ignoredColor: Exception that is not overridden. + func clearBackgroundColors(ignoring ignoredColor: UIColor? = nil) { + for subview in subviews { + subview.clearBackgroundColors(ignoring: ignoredColor) - var onFocusBackgroundColor: UIColor? - var onUnfocusBackgroundColor: UIColor? { - didSet { - viewBeforeUITitleView?.backgroundColor = onUnfocusBackgroundColor - } - } + if let ignoredColor, subview.backgroundColor == ignoredColor { + continue + } - override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) { - super.didUpdateFocus(in: context, with: coordinator) - if isFocused { - backgroundColor = onFocusBackgroundColor - coordinator.addCoordinatedAnimations({ [weak self] in - self?.viewBeforeUITitleView?.backgroundColor = self?.onFocusBackgroundColor - }) - } else { - backgroundColor = onUnfocusBackgroundColor - viewBeforeUITitleView?.backgroundColor = onUnfocusBackgroundColor + subview.backgroundColor = .clear } } } diff --git a/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/Common/SPNativeScreenViewController.swift b/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/Common/SPNativeScreenViewController.swift index d534cfc8c..2b6c5ab8d 100644 --- a/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/Common/SPNativeScreenViewController.swift +++ b/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/Common/SPNativeScreenViewController.swift @@ -194,11 +194,13 @@ class FocusGuideDebugView: UIView { button.setTitle(action.settings.text, for: .normal) button.setTitleColor(UIColor(hexString: style.onUnfocusTextColor), for: .normal) button.setTitleColor(UIColor(hexString: style.onFocusTextColor), for: .focused) - button.backgroundColor = UIColor(hexString: style.onUnfocusBackgroundColor) - button.onUnfocusBackgroundColor = button.backgroundColor - button.onFocusBackgroundColor = UIColor(hexString: style.onFocusBackgroundColor) button.titleLabel?.font = UIFont(from: style.font) - button.layer.cornerRadius = 12 + UIColor(hexString: style.onUnfocusBackgroundColor) + .map { getImageWithColor(color: $0) } + .map { button.setBackgroundImage($0, for: .normal) } + UIColor(hexString: style.onFocusBackgroundColor) + .map { getImageWithColor(color: $0) } + .map { button.setBackgroundImage($0, for: .focused) } } else { button.isHidden = true } From d5dacff813792b793675f2549a3cb3094499f339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maik=20A=C3=9Fhoff?= Date: Sun, 7 Jul 2024 13:45:19 +0200 Subject: [PATCH 2/5] Customize the background color of the category table view cells while keeping the rounded corners and focus effect (only affects tvOS >= 14). Background and text color are hardcoded for now since they are not included in view data from the backend. --- ...PRNativePrivacyManagerViewController.swift | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift b/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift index c247f204c..f6eae277f 100644 --- a/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift +++ b/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift @@ -58,7 +58,11 @@ protocol SPNativePrivacyManagerHome { setFocusGuidesForButtons() categoryTableView.accessibilityIdentifier = "Categories List" categoryTableView.allowsSelection = false - categoryTableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier) + if #available(tvOS 14.0, *) { + categoryTableView.register(CategoryCellView.self, forCellReuseIdentifier: cellReuseIdentifier) + } else { + categoryTableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier) + } categoryTableView.delegate = self categoryTableView.dataSource = self disableMenuButton() @@ -317,4 +321,32 @@ class FocusableScrollView: UIScrollView { } } +/// A table view cell with a custom background configuration. +/// It suppresses unwanted background and border effects that are automatically added by the system +/// while while keeping the drop shadow and scaling effect when being focused. +@available(tvOS 14.0, *) +class CategoryCellView: UITableViewCell { + + override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { + super.init(style: style, reuseIdentifier: reuseIdentifier) + setupBackground() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + private func setupBackground() { + var backgroundConfiguration = UIBackgroundConfiguration.listPlainCell() + backgroundConfiguration.backgroundColor = nil + self.backgroundConfiguration = backgroundConfiguration + } + + override func updateConfiguration(using state: UICellConfigurationState) { + super.updateConfiguration(using: state) + textLabel?.textColor = .white + clearBackgroundColors(ignoring: backgroundConfiguration?.backgroundColor) + } +} + // swiftlint:enable function_body_length From f1f2ce0631466ed0fadd3005b7d670ff5ae5002a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maik=20A=C3=9Fhoff?= Date: Sun, 7 Jul 2024 13:46:11 +0200 Subject: [PATCH 3/5] Use footnote text style for category cells to improve readability. --- .../GDPR/SPGDPRNativePrivacyManagerViewController.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift b/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift index f6eae277f..9753b8b30 100644 --- a/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift +++ b/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift @@ -300,6 +300,7 @@ extension SPGDPRNativePrivacyManagerViewController: UITableViewDataSource { let cell = categoryTableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath) cell.textLabel?.text = categories[indexPath.row].name.trimmingCharacters(in: .whitespacesAndNewlines) cell.textLabel?.setDefaultTextColorForDarkMode() + cell.textLabel?.font = .preferredFont(forTextStyle: .footnote) return cell } } From 6c972dd1fa42c46ac68ef4e50d457b390455d6d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maik=20A=C3=9Fhoff?= Date: Sun, 7 Jul 2024 14:36:34 +0200 Subject: [PATCH 4/5] Set the background color of the category cell to darkGray. --- .../GDPR/SPGDPRNativePrivacyManagerViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift b/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift index 9753b8b30..2552fb921 100644 --- a/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift +++ b/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift @@ -339,7 +339,7 @@ class CategoryCellView: UITableViewCell { private func setupBackground() { var backgroundConfiguration = UIBackgroundConfiguration.listPlainCell() - backgroundConfiguration.backgroundColor = nil + backgroundConfiguration.backgroundColor = .darkGray self.backgroundConfiguration = backgroundConfiguration } From 7070d98986c229d6418ae94b82897e70c2b03ed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maik=20A=C3=9Fhoff?= Date: Wed, 18 Sep 2024 10:13:33 +0200 Subject: [PATCH 5/5] Remove duplicate word in documentation --- .../GDPR/SPGDPRNativePrivacyManagerViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift b/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift index 2552fb921..92832b4da 100644 --- a/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift +++ b/ConsentViewController/Classes/Views/tvOS/NativePrivacyManager/GDPR/SPGDPRNativePrivacyManagerViewController.swift @@ -324,7 +324,7 @@ class FocusableScrollView: UIScrollView { /// A table view cell with a custom background configuration. /// It suppresses unwanted background and border effects that are automatically added by the system -/// while while keeping the drop shadow and scaling effect when being focused. +/// while keeping the drop shadow and scaling effect when being focused. @available(tvOS 14.0, *) class CategoryCellView: UITableViewCell {