Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

How to ignore keyboard safe area only for the last view in a VStack?

$
0
0

The keyboard lifts the last view in a VStack.

Banner ad lifted

Adding .ignoresSafeArea(.keyboard) only to BannerAd doesn't work. (Why?)

import SwiftUIimport RealmSwiftstruct ContentView: View {    @FocusState var focusedId: ObjectId?    var body: some View {        NavigationStack {            VStack {                ItemsView(focusedId: $focusedId)                BannerAd()            }            .ignoresSafeArea(.keyboard)        }    }}

If I add .ignoresSafeArea(.keyboard) to the VStack, the focus state doesn't work for the ItemsView. (correction: It just registered that the keyboard is simply lifting all views)

ItemsView not working

import SwiftUIimport RealmSwiftclass Item: Object, ObjectKeyIdentifiable {    @Persisted(primaryKey: true) var _id: ObjectId    @Persisted var name: String}struct ItemsView: View {    @ObservedResults(Item.self) var items    var focusedId: FocusState<ObjectId?>.Binding    var body: some View {        List {            // Items            ForEach(items) { item in                ItemView(item: item, focusedId: focusedId)            }            .onDelete(perform: $items.remove)        }        .navigationTitle("Items")        .toolbar {            // Add            ToolbarItem(placement: .navigationBarTrailing) {                Button {                    let item = Item()                    $items.append(item)                    focusedId.wrappedValue = item._id                } label: {                    Label("Add item", systemImage: "plus")                }            }            // Quick add            ToolbarItem(placement: .principal) {                Button {                    for i in 0...50 {                        let item = Item()                        item.name = "Item \(i)"                        $items.append(item)                    }                } label: {                    Text("Add multiple items")                }            }            // Delete            ToolbarItem(placement: .navigationBarLeading) {                Button(role: .destructive){                    $items.remove(atOffsets: IndexSet(integersIn: items.indices))                } label: {                    Label("Delete all", systemImage: "trash")                }            }        }    }}struct ItemView: View {    @ObservedRealmObject var item: Item    var focusedId: FocusState<ObjectId?>.Binding    var body: some View {        TextField("Item", text: $item.name)            .focused(focusedId, equals: item._id) // focus    }}
import SwiftUIstruct BannerAd: View {    var body: some View {        Text("Banner ad")            .padding()            .frame(maxWidth: .infinity)            .background(Color.green)    }}

Is there an easy way to ignore safe area only for BannerAd?


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>