我需要在 中获取渲染视图的宽度SwiftUI,这显然不是那么容易。
我的看法是,我需要一个返回视图尺寸的函数,就这么简单。
var body: some View {
VStack(alignment: .leading) {
Text(timer.name)
.font(.largeTitle)
.fontWeight(.heavy)
Text(timer.time)
.font(.largeTitle)
.fontWeight(.heavy)
.opacity(0.5)
}
}
Run Code Online (Sandbox Code Playgroud) .timer 文本样式将其扩展以填充宽度,而不是占用空间来容纳所包含的字符串。有办法改变这种行为吗?
\nText(entry.timeFinished, style: .timer).multilineTextAlignment(.leading).opacity(0.5).background(Color.red)\nRun Code Online (Sandbox Code Playgroud)\n\nText("3:41").opacity(0.5).background(Color.blue)\nRun Code Online (Sandbox Code Playgroud)\n\n整个视图:
\nstruct TimePieceWidgetEntryView: View {\n \n var entry: Provider.Entry\n \n var body: some View {\n VStack {\n Text("Timer \xe2\x8f\xb1")\n Text(Date().addingTimeInterval(600), style: .timer).opacity(0.5)\n }.font(.title.bold().monospacedDigit())\n .padding(5)\n .widgetURL(entry.url)\n }\n \n}\nRun Code Online (Sandbox Code Playgroud)\n 这是我上一个问题的扩展(在 SwiftUI 中使用获取视图的宽度)
我需要实现一个布局,其中每行的项目数是根据它们的组合宽度动态确定的(基本上,将项目排成一行,直到它们不再适合为止)。

有人告诉我,使用 GeometryReader 是一种用声明性语言做某事的hacky 方式,这显然是正确的。
我也被定向到这个类似 CollectionView 的组件https://github.com/Q-Mobile/QGrid但解决方案是静态的,因为在呈现任何组件之前,行数和每行单元格的数量是一次性确定的。
我不知道如何解决这个问题,所以任何建议对我来说都非常有价值!
??????
我有一个在应用程序中使用的 Core Data 数据模型,并且想向该数据模型添加一个我不一定想要存储的属性,因此我没有使用@NSManaged而是将该属性设置为@Published。
@Published var currentTime = "00:00"
Run Code Online (Sandbox Code Playgroud)
当然,在视图实例中,我使用@ObservedObject
@ObservedObject var timeItem: TimeItem
Run Code Online (Sandbox Code Playgroud)
在该视图中,我使用计时器来更新该值
.onReceive(Timer.publish(every: 0.015, on: .main, in: .common).autoconnect()) { time in
timeItem.currentTime = timeItem.timeFinished.timeIntervalSince(Date()).editableStringMilliseconds()
}
Run Code Online (Sandbox Code Playgroud)
但是,这不会触发视图更新。我不确定是否应该归咎于NSManagedObject ,但如果我将timeItem.currentTime值替换为本地 @State 值,则一切正常。
@State private var currentTime: String = "00:00"
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?
我目前正在尝试在我的应用程序中实现本地通知,但遇到了由于某种原因无法将徽章计数器增加到超过 1 的问题。
这是我配置和安排通知的方法。
func scheduleNotification() {
let content = UNMutableNotificationContent()
content.title = "\(self.title == "" ? "Title" : self.title) is done"
content.subtitle = "Tap to view"
content.sound = UNNotificationSound.default
content.badge = 1
if self.isPaused {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: self.currentTime, repeats: false)
let request = UNNotificationRequest(identifier: self.notificationIdentifier.uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
} else {
removeNotification()
}
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,当成功安排并确实发送了多个通知时,无论实际发送的通知数量如何,徽章计数器最多只会增加到 1。
有没有适当的方法来管理徽章计数,而这不是吗?