Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cute/ac7d03ae5a9d0429de6dcb6a3faa9f58 to your computer and use it in GitHub Desktop.
Save cute/ac7d03ae5a9d0429de6dcb6a3faa9f58 to your computer and use it in GitHub Desktop.
iOS 18 Widgets: Notes about updating the widget
import SwiftUI
import WidgetKit
@available(iOS, deprecated: 18.0, message: "use WidgetAccentedRenderingMode instead")
enum BackdeployedWidgetAccentedRenderingMode {
case accented
case accentedDesaturated
case desaturated
case fullColor
@available(iOS 18.0, *)
func toRenderingMode() -> WidgetAccentedRenderingMode {
switch self {
case .accented: .accented
case .accentedDesaturated: .accentedDesaturated
case .desaturated: .desaturated
case .fullColor: .fullColor
}
}
}
extension Image {
@available(iOS, deprecated: 18.0, message: "use widgetAccentedRenderingMode instead")
@ViewBuilder
func backdeployedWidgetAccentedRenderingMode(
_ mode: BackdeployedWidgetAccentedRenderingMode
) -> some View {
if #available(iOS 18.0, *) {
self.widgetAccentedRenderingMode(mode.toRenderingMode())
}
else {
self
}
}
}
// If you want to get black text on a white background,
// there is only one way out - to cut it out of the background
Circle()
.fill(.white)
.frame(width: 20, height: 20, alignment: .center)
.mask(
ZStack {
Circle()
.fill(Color.white)
Text(model.formattedDayNumber)
.font(.system(size: 9, weight: .bold))
.foregroundColor(.black)
}
.compositingGroup()
.luminanceToAlpha()
)
Circle()
.fill(model.isCurrent ? .white : .clear)
// No need for widgetAccentable(false) as it's not accentable by default
Text("21")
.font(.system(size: 15))
.foregroundColor(model.isCurrent ? .black : .white)
.widgetAccentable(model.isCurrent) // Accent only the current day
import SwiftUI
extension EnvironmentValues {
@available(iOS, deprecated: 16.0, message: "use `widgetRenderingMode` instead")
var isFullColorWidget: Bool {
get {
if #available(iOS 16.0, *) {
widgetRenderingMode == .fullColor
}
else {
self[FullColorWidgetKey.self]
}
}
}
}
private enum FullColorWidgetKey: EnvironmentKey {
static let defaultValue = true
}
Image("userAvatar")
.resizable()
.widgetAccentedRenderingMode(.fullColor)
struct PeridoDayView: View {
@Environment(\.widgetRenderingMode)
private var widgetRenderingMode
var body: some View {
DayRangeView()
.foregroundStyle(
widgetRenderingMode == .fullColor ? .periodColor : .white60Color
)
}
}
import SwiftUI
extension View {
@available(iOS, deprecated: 16.0, message: "use widgetAccentable(_:)` instead")
@ViewBuilder
func backdeployedWidgetAccentable(
_ accentable: Bool = true
) -> some View {
if #available(iOS 16.0, *) {
self
.widgetAccentable(accentable)
}
else {
self
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment