日期选择器按钮 / Swiftui
Posted
技术标签:
【中文标题】日期选择器按钮 / Swiftui【英文标题】:Date Picker Button / Swiftui 【发布时间】:2021-01-01 19:26:46 【问题描述】:我创建了一个日期选择器,但不知道如何在它下面放置按钮。 你能做到吗? swiftui 我正在学习新的东西。 我附上了示例设计图像。 我想要和图片一样。
@State private var zaman = Date()
@State private var tarihGorun = false
@State private var tarih = "Tarih Seçiniz"
DatePicker("",selection: $zaman,displayedComponents: .date).labelsHidden()
.accentColor(.white)
.frame(width: 300, height: 50, alignment: .center)
.font(Font.system(size: 25, design: .default))
.padding(5)
.font(Font.system(size: 15, weight: .medium, design: .serif))
.overlay(
RoundedRectangle(cornerRadius: 30)
.stroke(Color(red: 45 / 255, green: 0 / 255, blue: 112 / 255), lineWidth: 1))
.onTapGesture
self.tarihGorun = true
if tarihGorun
HStack
Button(action:
let zamanFormatter = DateFormatter()
zamanFormatter.dateFormat = "MM/dd/yyyy"
let alinanTarih = zamanFormatter.string(from: self.zaman)
self.tarih = alinanTarih
self.tarihGorun = false
)
Text("Tarih Seç")
Button(action:
self.tarihGorun = false
)
Text("Kapat").foregroundColor(Color.red)
【问题讨论】:
【参考方案1】:这是您本周第三次以上发布相同的问题。请不要转发问题。这是图像的示例,它只是一个常规的 DatePicker(),其下方有按钮。
import SwiftUI
struct FirstView: View
@State var showDatePicker: Bool = false
@State var savedDate: Date? = nil
var body: some View
ZStack
HStack
Text("Selected date: ")
Button(action:
showDatePicker.toggle()
, label:
Text(savedDate?.description ?? "SELECT DATE")
)
if showDatePicker
DatePickerWithButtons(showDatePicker: $showDatePicker, savedDate: $savedDate, selectedDate: savedDate ?? Date())
.animation(.linear)
.transition(.opacity)
struct DatePickerWithButtons: View
@Binding var showDatePicker: Bool
@Binding var savedDate: Date?
@State var selectedDate: Date = Date()
var body: some View
ZStack
Color.black.opacity(0.3)
.edgesIgnoringSafeArea(.all)
VStack
DatePicker("Test", selection: $selectedDate, displayedComponents: [.date])
.datePickerStyle(GraphicalDatePickerStyle())
Divider()
HStack
Button(action:
showDatePicker = false
, label:
Text("Cancel")
)
Spacer()
Button(action:
savedDate = selectedDate
showDatePicker = false
, label:
Text("Save".uppercased())
.bold()
)
.padding(.horizontal)
.padding()
.background(
Color.white
.cornerRadius(30)
)
struct DatePickerWithButtons_Previews: PreviewProvider
static var previews: some View
FirstView()
【讨论】:
感谢它的工作。你知道如何制作日期格式吗?你也可以帮忙吗? @nicksarno 查看***.com/questions/35700281/date-format-in-swift以上是关于日期选择器按钮 / Swiftui的主要内容,如果未能解决你的问题,请参考以下文章
如何更改GraphicalDatePickerStyle日期选择器Swiftui的选择颜色(AM,PM)和时间
如何知道用户在 SwiftUI 中的日期选择器中触摸了哪一天