首页 > 解决方案 > SwiftUI 列表/目标导航栏标题重影

问题描述

我在导航栏标题中遇到了一个奇怪的视觉错误。这个 swiftUI 应用程序有一个基本的主/详细类型视图,其中包含一个列表视图和一个带有 NavigationLink 的详细视图。当页面处于编辑模式时,该详细视图是显示信息的文本列表或文本字段列表。

当在具有编辑模式的详细视图中滚动到列表中的底部项目并对其进行编辑时,第一个视图上的 navigationBarTitle 具有第二个视图的“详细信息”navigationBarTitle 的幽灵。如果我不滚动重影不会出现。通过滚动,我包含滚动,因此键盘不会隐藏文本字段。

如果我将 Detail 视图 navigationBarTitle 更改为 .inline,问题是几乎无法察觉的闪光,没有永久残留。显然,我会将它保留在这种模式下,但我当然更喜欢标准的 iOS 外观。

我已经通过实验禁用了我能想到的所有组合 - 不高兴。

在此处输入图像描述

根列表:

NavigationView {
    List {
        ForEach(myPhysicians, id: \.self) { mp in
            NavigationLink(destination: PhysicianDetailView(physician: mp)) {
                Text(mp.lastName ?? "no last name")
            }
        }
        .onDelete { indexSet in
            //standard core data delete process
        }
    }.navigationBarTitle("Physicians")
        .navigationBarItems(
            leading:
            NavigationLink(destination: AddPhysicianView()) {
                Image(systemName: "plus.circle.fill")
                    .font(.title)
            },
            trailing:
            EditButton()
    )//bar button items
}//nav

详细视图:

var body: some View {
    
    DispatchQueue.main.async {
        self.localFirstName = self.physician.wrappedFirstName
        self.localLastName = self.physician.wrappedLastName
        //bunch more...
    }
    
    return ZStack(alignment: .topTrailing) {
        List {
            VStack { //group 1
                VStack (alignment: .leading) {
                    Text("First Name:")
                        .font(.system(size: 14))
                        .font(.subheadline)
                    if !showEditView {
                        Text("\(physician.wrappedFirstName)")
                            .frame(maxWidth: .infinity, alignment: .leading)
                            .padding(.vertical, 5)
                            .textFieldStyle(RoundedBorderTextFieldStyle())
                            .font(.headline)
                    } else {
                        TextField("tf firstname", text: $localFirstName)
                            .modifier(TextFieldSetup())
                    }
                }
                
                VStack (alignment: .leading) {
                    Text("Last Name:")
                        .font(.system(size: 14))
                        .font(.subheadline)
                    if !showEditView {
                        Text("\(physician.wrappedLastName)")
                            .frame(maxWidth: .infinity, alignment: .leading)
                            .padding(.vertical, 5)
                            .textFieldStyle(RoundedBorderTextFieldStyle())
                            .font(.headline)
                    } else {
                        TextField("tf lastname", text: $localLastName)
                            .modifier(TextFieldSetup())
                    }
                }
                //bunch more...
            }//outer vstack

        }//list
            .frame(maxWidth: .infinity - 20)
            //.navigationBarTitle("Detail",displayMode: .inline)
            .navigationBarTitle("Detail")
            
            .navigationBarBackButtonHidden(true)
            .navigationBarItems(
                leading:
                Button(action: {
                    self.presentationMode.wrappedValue.dismiss()
                }) {

                    if self.showEditView {
                        Text("Cancel")
                    } else {
                        Image(systemName: "arrowshape.turn.up.left.circle.fill")
                            .font(.title)
                    }
                },
                trailing:
                Button(action: {
                    if self.showEditView {
                        self.saveEditedPhysicianToCoreData(physician: self.physician)
                    }
                    self.showEditView.toggle()
                }) {
                    Image(systemName: self.showEditView ? "gear" : "square.and.pencil")
                        .font(.title)
                    .frame(width: 60, height: 60)
                }
            )//nav bar items
    }//zstack
    .modifier(AdaptsToSoftwareKeyboard())
    
}//body

任何指导将不胜感激。Xcode 11.6 iOS 13.6

添加:2020 年 7 月 29 日

struct AdaptsToSoftwareKeyboard: ViewModifier {

    @State var currentHeight: CGFloat = 0

    func body(content: Content) -> some View {
        content
            .padding(.bottom, self.currentHeight)
            .edgesIgnoringSafeArea(self.currentHeight == 0 ? Edge.Set() : .bottom)
            .onAppear(perform: subscribeToKeyboardEvents)
    }

    private let keyboardWillOpen = NotificationCenter.default
        .publisher(for: UIResponder.keyboardWillShowNotification)
        .map { $0.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect }
        .map { $0.height }

    private let keyboardWillHide =  NotificationCenter.default
        .publisher(for: UIResponder.keyboardWillHideNotification)
        .map { _ in CGFloat.zero }

    private func subscribeToKeyboardEvents() {
        _ = Publishers.Merge(keyboardWillOpen, keyboardWillHide)
            .subscribe(on: RunLoop.main)
            .assign(to: \.self.currentHeight, on: self)
    }
}

标签: iosxcodeswiftui

解决方案


推荐阅读