swiftui-.hourandminute仅显示“ 00”和&quot“ 30'分钟

发布于 2025-01-22 08:18:12 字数 452 浏览 4 评论 0原文

我希望我的头衔足够解释。我有一个小时的时间选择器,每分钟都会单独显示。我想要的是仅显示“ 00”和“ 30”分钟。这是可以实现的吗?

我在时间选择器中使用的代码:

DatePicker("", selection: $selectedStartHour, displayedComponents: .hourAndMinute)
    .datePickerStyle(CompactDatePickerStyle())
    .labelsHidden()
    .clipped()

”在此处输入图像说明”

I hope my title is self explanatory enough. I have an hours and minutes time picker, where every minute is shown individually. What I want is to only show the "00" and "30" minutes. Is this achievable?

The code I use for the time picker:

DatePicker("", selection: $selectedStartHour, displayedComponents: .hourAndMinute)
    .datePickerStyle(CompactDatePickerStyle())
    .labelsHidden()
    .clipped()

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2025-01-29 08:18:12

发表的评论是错误的。当然,您可以使用一个插件的代码来执行此操作:

UIDatePicker.appearance().minuteInterval = 30

问题是,整个应用程序中的所有时间选择器现在都将以30分钟的增量增量。为了减轻这一点,您使用.onappear和.ondisappear如下:

.onAppear {
    UIDatePicker.appearance().minuteInterval = 30
}

.onDisappear {
    UIDatePicker.appearance().minuteInterval = 1
}

现在,当您的视图出现时,选择器将设置为30分钟的增量。当视图消失时,它将设置回1分钟增量的默认值。

现在,您可以轻松地控制按观看次数显示的时间增量。

The comment posted is wrong. You can of course do this with a one line piece of code:

UIDatePicker.appearance().minuteInterval = 30

The issue is that now all of your time pickers throughout your entire app will be in 30 minute increments. To alleviate this, you use .onAppear and .onDisappear as follows:

.onAppear {
    UIDatePicker.appearance().minuteInterval = 30
}

.onDisappear {
    UIDatePicker.appearance().minuteInterval = 1
}

Now, when your view appears the picker will be set to 30 minute increments. When the view disappears, it will be set back to the default of 1 minute increments.

Now you can easily control what time increment is displayed on a per view basis.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文