From dccfb4e831393f3369088d6224bd69abe67ffab2 Mon Sep 17 00:00:00 2001 From: Jed Fox Date: Thu, 15 Dec 2022 07:42:10 -0500 Subject: [PATCH] Avoid division by 0 --- .../MastodonUI/Extension/UIScreen.swift | 18 ++++++++++++++++++ .../View/Content/StatusCardControl.swift | 6 +++--- 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 MastodonSDK/Sources/MastodonUI/Extension/UIScreen.swift diff --git a/MastodonSDK/Sources/MastodonUI/Extension/UIScreen.swift b/MastodonSDK/Sources/MastodonUI/Extension/UIScreen.swift new file mode 100644 index 000000000..831f2cb32 --- /dev/null +++ b/MastodonSDK/Sources/MastodonUI/Extension/UIScreen.swift @@ -0,0 +1,18 @@ +// +// UIScreen.swift +// +// +// Created by Jed Fox on 2022-12-15. +// + +import UIKit + +extension UIScreen { + public var pixelSize: CGFloat { + if scale > 0 { + return 1 / scale + } + // should never happen but just in case + return 1 + } +} diff --git a/MastodonSDK/Sources/MastodonUI/View/Content/StatusCardControl.swift b/MastodonSDK/Sources/MastodonUI/View/Content/StatusCardControl.swift index 0a175b23b..bd2485cf4 100644 --- a/MastodonSDK/Sources/MastodonUI/View/Content/StatusCardControl.swift +++ b/MastodonSDK/Sources/MastodonUI/View/Content/StatusCardControl.swift @@ -184,8 +184,8 @@ public final class StatusCardControl: UIControl { super.didMoveToWindow() if let window = window { - layer.borderWidth = 1 / window.screen.scale - dividerConstraint?.constant = 1 / window.screen.scale + layer.borderWidth = window.screen.pixelSize + dividerConstraint?.constant = 1 / window.screen.pixelSize } } @@ -196,7 +196,7 @@ public final class StatusCardControl: UIControl { NSLayoutConstraint.deactivate(layoutConstraints) dividerConstraint?.deactivate() - let pixelSize = 1 / (window?.screen.scale ?? 1) + let pixelSize = 1 / (window?.screen.pixelSize ?? 1) switch layout { case .large(let aspectRatio): containerStackView.alignment = .fill