我正在使用故事板制作一个应用程序,稍后再添加小部件。我在主屏幕上的小部件变灰了。
的数据
在小部件抽屉中打开时看起来很好。
下面是我的小部件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))
}
}
型
所以我的问题是我如何解决这个问题,使它正确地出现在主屏幕上,就像它显示在小工具抽屉?
注意它在模拟器上工作正常,但由于某种原因在设备上不工作。
2条答案
按热度按时间yzxexxkh1#
小部件的允许内存是有限的,我减少了我使用的图像文件的大小,它现在工作正常。
t2a7ltrp2#
iOS扩展插件(今日)灰色背景
Widget有各种各样的限制,如果你遇到其中一个,你会看到一个灰色的背景
举例来说:
[存档]由于图像太大,Widget存档失败[2] -(2317,929)。
App Extension(e.g Widget aka Today, Photo Editing...)(https://stackoverflow.com/a/66520509/4770877)