10

I'm trying to get the right widget family in a view, but I'm always getting .systemMedium value. This is my view:

import SwiftUI

struct MyView: View
{
    @Environment(\.widgetFamily) var family

    let entry: MyEntry

    @ViewBuilder
    var body: some View
    {
        switch family
        {
        case .systemSmall:
           Text("systemSmall")
        case .systemMedium:
           Text("systemMedium")
        case .systemLarge:
           Text("systemLarge")
        case .systemExtraLarge:
           Text("systemExtraLarge")
        }
    }
}

Any ideas why this is happening? Thanks.

iOS Dev
  • 4,143
  • 5
  • 30
  • 58
  • did you set up the config? `var body: some WidgetConfiguration { StaticConfiguration(kind: kind, provider: Provider()) { entry in MyViewEntry(entry: entry) } .configurationDisplayName("Name") .description("Descr") .supportedFamilies([.systemSmall, .systemMedium, .systemLarge]) }` In particular: `.supportedFamilies([.systemSmall, .systemMedium, .systemLarge])` – Kateryna Gridina Nov 23 '21 at 13:11
  • Yes, I did it this way. – iOS Dev Nov 24 '21 at 07:11
  • 2
    Same problem here. This used to work with Xcode 12. – shady Dec 30 '21 at 15:19
  • 1
    Looks like a bug in SwiftUI Previews. I have filed feedback regarding this. FB9950816. You should feedback as well! It works fine when ran on Simulator or real device. – rahulrs Mar 09 '22 at 06:44
  • I've litteratly just written the same code :) -- its still an issue in xcode 13.2.1 (I'm still on Big Sur) – Jens Peter Apr 23 '22 at 10:43
  • Bug still present in 13.3.1 – nylki May 10 '22 at 13:46

1 Answers1

1

Cautionary tale, do NOT copy the below code blindly!

For me, at first this was happening for the previews in Xcode, so I searched around and implemented these extensions:

// DEBUG
extension WidgetFamily: EnvironmentKey {
    public static var defaultValue: WidgetFamily = .systemSmall
}

extension EnvironmentValues {
  var widgetFamily: WidgetFamily {
    get { self[WidgetFamily.self] }
    set { self[WidgetFamily.self] = newValue }
  }
}

as well as specifying the family environment directly for the previews:

Group {
   SWidgetEntryView(entry: SimpleEntry(date: Date()))
     .previewContext(WidgetPreviewContext(family: .systemSmall))
     .environment(\.widgetFamily, .systemSmall)
   SWidgetEntryView(entry: SimpleEntry(date: Date()))
     .previewContext(WidgetPreviewContext(family: .systemMedium))
     .environment(\.widgetFamily, .systemMedium)
}

This will solve the issue during preview, but will mess up the widgets on the device, as it overrides the widgetFamily environment variable. All widget sizes will have the incorrect family environment that you specify for defaultValue.

If this kind of problem is happening for you on the device, you probably implemented a similar extension to make previews work. Removing it will fix the issue on the device. I'm not sure what's the correct fix for the previews.

Zoltán Matók
  • 3,923
  • 2
  • 33
  • 64