Skip to content

Instantly share code, notes, and snippets.

@kylebshr
Last active November 7, 2023 18:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylebshr/6896bf23e73bdb48460ace1093795f86 to your computer and use it in GitHub Desktop.
Save kylebshr/6896bf23e73bdb48460ace1093795f86 to your computer and use it in GitHub Desktop.
//
// StandByMargins.swift
//
// Created by Kyle Bashour on 8/29/23.
//
import SwiftUI
extension View {
func standByMargins() -> some View {
modifier(StandByMargins())
}
}
private struct StandByMargins: ViewModifier {
@Environment(\.widgetContentMargins) private var margins
private var isInStandby: Bool {
margins.leading > 0 && margins.leading < 5
}
func body(content: Content) -> some View {
content.padding(isInStandby ? margins : EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
}
}
@kylebshr
Copy link
Author

kylebshr commented Aug 29, 2023

If you have full-bleed content in StandBy, it currently gets clipped. Applying the margins again seems to perfectly fit the content within the rounded rect. This modifier should only apply the margins in StandBy - every other location has margins that are zero (iPhone / iPad Lock Screen) or greater than 5 (e.g., rectangular on watchOS is 7 pts).

Example:

Without .standByMargins() With .standByMargins()
Simulator Screenshot - iPhone 14 Pro - 2023-08-29 at 11 35 09 Simulator Screenshot - iPhone 14 Pro - 2023-08-29 at 11 35 34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment