应用小部件仅在主屏幕上显示为灰色

Posted

技术标签:

【中文标题】应用小部件仅在主屏幕上显示为灰色【英文标题】:App Widgets are greyed out on home screen only 【发布时间】:2021-06-10 06:51:44 【问题描述】:

我正在使用情节提要制作一个应用程序,并稍后向其中添加小部件。 我的主屏幕上的小部件呈灰色显示。

在小部件抽屉中打开时看起来不错。

这是我的小部件 UI 代码

ZStack 
        Image(affirmationBGImage)
            .resizable()
            .scaledToFill()
            .edgesIgnoringSafeArea(.all)
        HStack 
            Spacer(minLength: 5)
            HStack 
                Text(affirmationText)
                    .font(.headline)
                    .foregroundColor(.black)
            
            .background(Color.white.opacity(0.2))
            .cornerRadius(5)
            Spacer(minLength: 5)
        
        .padding(0)
    

这也是我的时间线类代码

import WidgetKit
import SwiftUI
import Intents

struct Provider: IntentTimelineProvider 
    func placeholder(in context: Context) -> SimpleEntry 
        SimpleEntry(date: Date(), affirmationDetails: AffirmationWidgetDetails.random(), configuration: ConfigurationIntent())
    
    
    func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) 
        let entry = SimpleEntry(date: Date(), affirmationDetails: AffirmationWidgetDetails.random(), configuration: configuration)
        completion(entry)
    
    
    func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) 
        var entries: [SimpleEntry] = []
        
        // Generate a timeline consisting of five entries an hour apart, starting from the current date.
        let currentDate = Date().zeroSeconds
        for hourOffset in 0 ..< 60 
            let entryDate = Calendar.current.date(byAdding: .minute, value: hourOffset, to: currentDate!)!
            let entry = SimpleEntry(date: entryDate, affirmationDetails: AffirmationWidgetDetails.random(), configuration: configuration)
            entries.append(entry)
        
        
        let timeline = Timeline(entries: entries, policy: .atEnd)
        completion(timeline)
    


struct SimpleEntry: TimelineEntry 
    let date: Date
    let affirmationDetails: AffirmationWidgetDetails
    let configuration: ConfigurationIntent


struct AffirmationWidgetEntryView: View 
    
    var entry: Provider.Entry
    
    var body: some View 
        AffirmationWidgetView(affirmationDetails: entry.affirmationDetails)
    
    


@main
struct AffirmationWidget: Widget 
    let kind: String = "AffirmationWidget"
    
    var body: some WidgetConfiguration 
        IntentConfiguration(
            kind: kind,
            intent: ConfigurationIntent.self,
            provider: Provider()
        )  entry in
            AffirmationWidgetEntryView(entry: entry)
        
        .configurationDisplayName("Random Affirmation")
        .description("Displays a widget with an affirmation")
        .supportedFamilies([.systemLarge, .systemMedium, .systemSmall])
    


struct AffirmationWidget_Previews: PreviewProvider 
    static var previews: some View 
        AffirmationWidgetEntryView(entry: SimpleEntry(date: Date(), affirmationDetails: AffirmationWidgetDetails.random(), configuration: ConfigurationIntent()))
            .previewContext(WidgetPreviewContext(family: .systemSmall))
    

所以我的问题是如何解决这个问题,以便它在主屏幕上正确显示,就像它在小部件抽屉上显示一样?

注意它在模拟器上运行良好,但由于某种原因在设备上运行良好。

【问题讨论】:

【参考方案1】:

小部件允许的内存是有限的,我减小了我正在使用的图像文件的大小,现在它工作正常。

【讨论】:

以上是关于应用小部件仅在主屏幕上显示为灰色的主要内容,如果未能解决你的问题,请参考以下文章

将应用小部件放置在 Activity 之外的主屏幕上

AppWidget(桌面小部件)

AppWidget(桌面小部件)

AppWidget(桌面小部件)

在主屏幕小部件的列表视图上方添加一个按钮 - 如何捕获单击此按钮?

如何在特定时间内显示小部件?