English 中文(简体)
在电网内调取日期
原标题:Aligning a date picker within a grid

我有快速倡议的观点,提出了若干<代码>。 GridRows in a Grid. 每一行各有2栏,左栏为统一,右栏为左栏。 每一行都有标题(左栏)和数据领域(右栏)。

<代码>Text Fields和Pickers按我的期望行事,与栏左侧一致。 但<代码>DatePicker将只与其栏右侧一致。

我曾尝试过:

  • Placing a Spacer() after the DatePicker
  • Putting the DatePicker inside VStack(alignment: .leading) within its GridRow

任何人都知道如何将日期推向左边的其他牢房?

如下:

“在座的影像描述”/</a

import SwiftUI

struct ContentView: View {
    
    @State var name = ""
    @State var date = Date()
    @State var house = Int()
    
    var body: some View {
        NavigationStack {
            ScrollView {
                VStack(alignment: .leading) {
                    Grid(alignment: .leading) {
                        GridRow {
                            Text("Name (Image(systemName: "person")):")
                                .font(.headline)
                                .gridColumnAlignment(.trailing)
                            TextField("Full name", text: $name)
                                .font(.body)
                                .padding(.horizontal, 10)
                                .padding(.vertical, 5)
                                .background(Color(UIColor.systemBackground))
                                .cornerRadius(8)
                                .gridColumnAlignment(.leading)
                        }
                        GridRow {
                            Text("Birthdate (Image(systemName: "calendar")):")
                                .font(.headline)
                            DatePicker("", selection: $date, displayedComponents: .date)
                        }
                        GridRow {
                            Text("House (Image(systemName: "house")):")
                                .font(.headline)
                            Picker("", selection: $house) {
                                Text("Gryffindor").tag(1)
                                Text("Slytherin").tag(2)
                                Text("Ravenclaw").tag(3)
                                Text("Hufflepuff").tag(4)
                            }
                        }
                    }
                }
                .padding(10)
                .background(RoundedRectangle(cornerRadius: 10)
                    .fill(Color.gray
                        .gradient
                        .opacity(0.15)))
            }
            .padding(.horizontal)
            .navigationTitle("Add Wizard Info")
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}
问题回答

By inspecting the UI hierarchy, SwiftUI seems to put the date picker inside an HStack, consisting of the label (showing an empty string in your case), a Spacer, and a UIViewRepresentable representing a UIDatePicker.

“entergraph

(Red is the HStack, Blue is the Spacer , Black is the UIViewRepresentable Packping a UIDatePicker. 标签表明,“也看不到”。

使整个“expand”横向化,将日期推向正确。

仅添加https://developer.apple.com/documentation/swiftui/datepicker” rel=“nofollow noreferer”>

DatePicker("", selection: $date, displayedComponents: .date)
    .fixedSize()

如今, 升幅不大,但注意到仍然有节奏:

“entergraph

这就是“条码”的缺省。 HStack在标签和日期选择之间添加。 如果你也想删除,你也可以利用以下事实:,即“标签内容”(但不能确保这一点得到保证),并适用你自己的<代码>。

DatePicker("", selection: $date, displayedComponents: .date)
    .labeledContentStyle(ContentOnlyStyle())
// this removes the label, and only shows the content (in this case, the date picker)
struct ContentOnlyStyle: LabeledContentStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.content
    }
}

现在,你甚至不需要<代码>固定





相关问题
asp.net gridview editindex

Please take a look at this: protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int index = Convert.ToInt32(e.CommandArgument); if (e.CommandName == "Edit") {...

How do I stop my gridview from caching?

I search for results, click one, it opens in a new window, then edit it, and close the popup. Then I hit search to refresh my gridview, but the changes do not reflect unless I hit F5. It s caching ...

get data from gridview without querying database

I am new at this so please bear with me... I have managed to get the following code to work...so when I click on the "select" link in each row of the gridview, the data is transfered to other label/...

WPF GridView, Display GetType().Name in DisplayMemberBinding

I want include the Type name of each object in my collection from my GridView. I have a collection which has four different types in it that all derive from a base class. Call them, Foo, Bar, Fizz, ...

GridView that toggles percentages of counts

I am using a SqlDataSource that returns a table of raw counts. One of these columns is the "total". I would like to give the user the ability to show these counts as a percentage of total using some ...

热门标签