TabBar icon indicating copy to clipboard operation
TabBar copied to clipboard

Content is hidden behind TabBar

Open twodayslate opened this issue 2 years ago • 5 comments

Simulator Screen Shot - iPhone 14 Pro - 2023-01-23 at 19 53 20

diff --git a/Example/Example/ContentView.swift b/Example/Example/ContentView.swift
index 5b7e8e3..8e591c2 100644
--- a/Example/Example/ContentView.swift
+++ b/Example/Example/ContentView.swift
@@ -61,14 +61,23 @@ struct ContentView: View {
                 Text("Hide/Show TabBar")
             }
             .tabItem(for: Item.first)
-            
-            Text("Second")
-                .tabItem(for: Item.second)
+
+            ScrollView {
+                VStack {
+                    ForEach(0..<100, id: \.self) { i in
+                        Rectangle()
+                            .fill(Color.accentColor)
+                            .overlay(
+                                TextField("\(i)", text: .constant("\(i)"))
+                            )
+                    }
+                }
+            }
+            .tabItem(for: Item.second)
             
             Text("Third")
                 .tabItem(for: Item.third)
         }
-        .tabBar(style: CustomTabBarStyle())
         .tabItem(style: CustomTabItemStyle())
     }
 }

twodayslate avatar Jan 24 '23 00:01 twodayslate

Hello, @twodayslate!

I will look into this issue on my free time

Thanks :)

onl1ner avatar Jan 27 '23 15:01 onl1ner

Since the TabBar can also be floating, by default, content can fall behind it. If you don't want that behaviour, add a bottom padding of the height of your TabBar.

@onl1ner maybe it would be a nice feature to be able to specify it as not floating and automatically modify the content's area.

dkk avatar Jul 31 '23 07:07 dkk

My solution was based on ChildSizeReader I found somewhere on SO + modified TabView + added .edgesIgnoringSafeArea([.bottom, .leading, .trailing]) to support landscape device orientation

struct ChildSizeReader<Content: View>: View {
  @Binding var size: CGSize
  let content: () -> Content
  var body: some View {
    ZStack {
      content()
        .background(
          GeometryReader { proxy in
            Color.clear
              .preference(key: SizePreferenceKey.self, value: proxy.size)
          }
        )
    }
    .onPreferenceChange(SizePreferenceKey.self) { preferences in
      self.size = preferences
    }
  }
}

struct SizePreferenceKey: PreferenceKey {
  typealias Value = CGSize
  static var defaultValue: Value = .zero

  static func reduce(value _: inout Value, nextValue: () -> Value) {
    _ = nextValue()
  }
}

public struct TabBar<TabItem: Tabbable, Content: View>: View {
  ...
  @State var barSize: CGSize = .zero
  public var body: some View {
    return ZStack {
      self.content
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .environmentObject(self.selectedItem).edgesIgnoringSafeArea(.bottom)
        .padding(.bottom, barSize.height)
        .edgesIgnoringSafeArea(.bottom)

      GeometryReader { geometry in
        VStack {
          Spacer()
          ChildSizeReader(size: $barSize) {
            self.tabBarStyle.tabBar(with: geometry) {
              .init(self.tabItems)
            }
          }
        }
        .edgesIgnoringSafeArea([.bottom, .leading, .trailing])
        .visibility(self.visibility)
      }
    }
    .onPreferenceChange(TabBarPreferenceKey.self) { value in
      self.items = value
    }
  }
}

igavrysh avatar Feb 15 '24 01:02 igavrysh

'safeAreaInset(edge:alignment:spacing:content:)' should work but it is only available in > iOS 15.0

MartinSandeCosta avatar Apr 18 '24 14:04 MartinSandeCosta

My solution was based on ChildSizeReader I found somewhere on SO + modified TabView + added .edgesIgnoringSafeArea([.bottom, .leading, .trailing]) to support landscape device orientation

struct ChildSizeReader<Content: View>: View {
  @Binding var size: CGSize
  let content: () -> Content
  var body: some View {
    ZStack {
      content()
        .background(
          GeometryReader { proxy in
            Color.clear
              .preference(key: SizePreferenceKey.self, value: proxy.size)
          }
        )
    }
    .onPreferenceChange(SizePreferenceKey.self) { preferences in
      self.size = preferences
    }
  }
}

struct SizePreferenceKey: PreferenceKey {
  typealias Value = CGSize
  static var defaultValue: Value = .zero

  static func reduce(value _: inout Value, nextValue: () -> Value) {
    _ = nextValue()
  }
}

public struct TabBar<TabItem: Tabbable, Content: View>: View {
  ...
  @State var barSize: CGSize = .zero
  public var body: some View {
    return ZStack {
      self.content
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .environmentObject(self.selectedItem).edgesIgnoringSafeArea(.bottom)
        .padding(.bottom, barSize.height)
        .edgesIgnoringSafeArea(.bottom)

      GeometryReader { geometry in
        VStack {
          Spacer()
          ChildSizeReader(size: $barSize) {
            self.tabBarStyle.tabBar(with: geometry) {
              .init(self.tabItems)
            }
          }
        }
        .edgesIgnoringSafeArea([.bottom, .leading, .trailing])
        .visibility(self.visibility)
      }
    }
    .onPreferenceChange(TabBarPreferenceKey.self) { value in
      self.items = value
    }
  }
}

Saved my life. 👍🏻

kenzo-ai avatar Jun 04 '24 10:06 kenzo-ai