From 1f443b9cc84bd79ddead972a9f5e0ac6e4bbd120 Mon Sep 17 00:00:00 2001 From: Nicholas Entin Date: Fri, 26 Jan 2024 22:37:50 -0800 Subject: [PATCH] Get scale factor for view from trait collection instead of screen --- Paralayout/PixelRounding.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Paralayout/PixelRounding.swift b/Paralayout/PixelRounding.swift index 876b9c2..3ac4f17 100644 --- a/Paralayout/PixelRounding.swift +++ b/Paralayout/PixelRounding.swift @@ -34,7 +34,17 @@ extension UIScreen: ScaleFactorProviding { extension UIView: ScaleFactorProviding { public var pixelsPerPoint: CGFloat { + let scaleFromTraits = traitCollection.displayScale + + // The trait collection is the authority for display scale, but sometimes the trait collection does not have a + // value for the scale, in which case it will return 0, breaking all the things. + if scaleFromTraits > 0 { + return scaleFromTraits + } + + #if os(iOS) return (window?.screen ?? UIScreen.main).pixelsPerPoint + #endif } }