本文介绍了SwiftUI 列表正在使用 .navigationBarItems 进行剪辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我添加 .navigationBarItems 修饰符列表时,它正在裁剪并且不会像这样占用所有空间

When I add .navigationBarItems modifier list is clipping and doesn't take all space like this

它应该像这张照片(没有 .navigationBarItems):

It should be like on this photo (without .navigationBarItems):

代码

struct PatientsListView: View {
    @ObservedObject var listData = PatientsListViewModel()
    var body: some View {
        NavigationView{
            Group {
                if listData.patientsList.count > 0{
                    List{
                        ForEach(Array(listData.patientsList)) {  patient in
                            NavigationLink(destination: PatientsDetailView(patient: patient),
                                           label: {
                                            PatientsListRow(patient: patient)
                                           })
                        }
                    }
                }
                else if listData.patientsList.count == 0 {
                    Text("Самое время добавить пациентов!").foregroundColor(.gray)
                    
                }
                else if listData.isLoading {
                    ProgressView()
                }
            }
            .navigationTitle("Пациенты")
            .navigationBarTitleDisplayMode(.large)
            .navigationBarItems(trailing: NavigationLink(destination: PatientCreateView(), label: {
                Image(systemName: "plus").foregroundColor(.white)
            }))
            
            
            
        }
        .onAppear(perform: {
            listData.fetchPatients()
        })
    }
}

推荐答案

这是默认行为.如果您愿意,可以显式设置 List 样式,例如

This is default behavior. If you like you can set List style explicitly, like

List{
    ForEach(Array(listData.patientsList)) {  patient in
        NavigationLink(destination: PatientsDetailView(patient: patient),
                       label: {
                        PatientsListRow(patient: patient)
                       })
    }
}
.listStyle(PlainListStyle())        // << here !!

这篇关于SwiftUI 列表正在使用 .navigationBarItems 进行剪辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 12:54