Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content is hidden behind TabBar #15

Open
twodayslate opened this issue Jan 24, 2023 · 5 comments
Open

Content is hidden behind TabBar #15

twodayslate opened this issue Jan 24, 2023 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@twodayslate
Copy link

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())
     }
 }
@onl1ner
Copy link
Owner

onl1ner commented Jan 27, 2023

Hello, @twodayslate!

I will look into this issue on my free time

Thanks :)

@onl1ner onl1ner added the bug Something isn't working label Jan 27, 2023
@onl1ner onl1ner self-assigned this Jan 27, 2023
@dkk
Copy link

dkk commented Jul 31, 2023

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.

@igavrysh
Copy link

igavrysh commented Feb 15, 2024

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
    }
  }
}

@MartinSandeCosta
Copy link

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

@kenzo-ai
Copy link

kenzo-ai commented Jun 4, 2024

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 added a commit to firstorderai/SwiftUITabBar that referenced this issue Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants