実現したいこと
遷移時に重なるViewの解消
前提
ビルドはできます。
発生している問題・エラーメッセージ
エラーメッセージなし Viewが重なる
該当のソースコード
SwiftUI
1 2struct ContentView: View { 3 @Environment(\.managedObjectContext) private var viewContext 4 5 @FetchRequest( 6 sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)], 7 animation: .default) 8 private var items: FetchedResults<Item> 9 10 private let list1 = [ 11 SampleStruct(sectionTitle: "A", listContent: ["elementA1"],systemNameText: "pin", folderRowName: "a"), 12 SampleStruct(sectionTitle: "B", listContent: ["elementB1"],systemNameText: "pin", folderRowName: "a"), 13 SampleStruct(sectionTitle: "C", listContent: ["elementC1"],systemNameText: "pin", folderRowName: "a"), 14 ] 15 16 var body: some View { 17 NavigationView { 18 List { 19 ForEach(list1) { item in 20 Section(item.sectionTitle) { 21 ForEach(item.listContent, id: \.self) { item2 in NavigationLink{ArrayView()}label: { 22 SampleStruct(sectionTitle: "A", listContent: ["elementA1"],systemNameText: "pin", folderRowName: "a")} 23 } 24 .toolbar { 25 ToolbarItem(placement: .navigationBarTrailing) { 26 EditButton() 27 } 28 } 29 } 30 } 31 } 32 .navigationTitle("A") 33 } 34 } 35} 36 37struct SampleStruct : View, Identifiable { 38 let id = UUID() 39 let sectionTitle: String 40 let listContent: [String] 41 let systemNameText: String 42 let folderRowName: String 43 var body: some View { 44 HStack{ 45 Image(systemName: systemNameText) 46 Text(folderRowName) 47 Text("\(systemNameText.count)") 48 } 49 } 50} 51 52struct ArrayView: View { 53 @Environment(\.managedObjectContext) private var viewContext 54 55 @FetchRequest( 56 sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: false)], 57 animation: .default) 58 private var items: FetchedResults<Item> 59 60 var body: some View { 61 NavigationView { 62 List { 63 ForEach(items) { item in 64 NavigationLink { 65 AnotherView(item: item) 66 } label: { 67 Text(item.text ?? "") 68 } 69 } 70 .onDelete(perform: deleteItems) 71 } 72 .toolbar { 73#if os(iOS) 74 ToolbarItem(placement: .navigationBarTrailing) { 75 EditButton() 76 } 77#endif 78 ToolbarItem { 79 Button(action: addItem) { 80 Label("Add Item", systemImage: "plus") 81 } 82 } 83 } 84 Text("Select an item") 85 } 86 } 87 88 private func addItem() { 89 withAnimation { 90 let newItem = Item(context: viewContext) 91 newItem.timestamp = Date() 92 93 94 do { 95 try viewContext.save() 96 } catch { 97 // Replace this implementation with code to handle the error appropriately. 98 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 99 let nsError = error as NSError 100 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") 101 } 102 } 103 } 104 105 private func deleteItems(offsets: IndexSet) { 106 withAnimation { 107 offsets.map { items[$0] }.forEach(viewContext.delete) 108 109 do { 110 try viewContext.save() 111 } catch { 112 // Replace this implementation with code to handle the error appropriately. 113 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 114 let nsError = error as NSError 115 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") 116 } 117 } 118 } 119} 120 121private let itemFormatter: DateFormatter = { 122 let formatter = DateFormatter() 123 formatter.dateStyle = .short 124 formatter.timeStyle = .medium 125 return formatter 126}() 127 128 129struct AnotherView: View { 130 131 @Environment(\.managedObjectContext) private var viewContext 132 @State var textEditorText = "" 133 var item: Item 134 135 init(item: Item) { _textEditorText = State(initialValue: item.text ?? "") 136 self.item = item } 137 138var body: some View { 139TextEditor(text: $textEditorText).frame(width: .infinity, height: .infinity) 140 141.toolbar { ToolbarItem(placement: .navigationBarLeading){ Button(action: {additem()}) {Image(systemName: "heart")}} } 142} 143 144func additem () { 145withAnimation { 146 item.text = textEditorText 147 148 do { 149 try viewContext.save() 150 } catch { 151 let nsError = error as NSError 152 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") 153 } 154 } 155 }} 156 157struct ContentView_Previews: PreviewProvider { 158 static var previews: some View { 159 ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) 160 } 161} 162
試したこと
ZStackで囲ってみました。
補足情報(FW/ツールのバージョンなど)
Xcode14.1
追記
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/04/04 10:00
2023/04/04 10:01