Last active
July 10, 2020 09:25
-
-
Save quarrant/5a42b918d1fefc85128d9328d0bd70a9 to your computer and use it in GitHub Desktop.
TabViewWrapper.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TabView+TabBarHidden.swift | |
// Burnout | |
// | |
// Created by user on 09.07.2020. | |
// Copyright © 2020 quarrant. All rights reserved. | |
// | |
import SwiftUI | |
struct TabViewTabBarHidden: ViewModifier { | |
init(_ isHidden: Bool) { | |
UITabBar.appearance().isHidden = isHidden | |
} | |
func body(content: Content) -> some View { | |
content | |
} | |
} | |
extension TabView { | |
func tabBarHidden(_ isHidden: Bool) -> some View { | |
self.modifier(TabViewTabBarHidden(isHidden)) | |
} | |
} | |
// | |
// TabViewWrapper.swift | |
// Burnout | |
// | |
// Created by user on 09.07.2020. | |
// Copyright © 2020 quarrant. All rights reserved. | |
// | |
import SwiftUI | |
struct TabButton: View { | |
let icon: String | |
let isSelected: Bool | |
var body: some View { | |
Image(systemName: self.icon) | |
.resizable() | |
.aspectRatio(contentMode: .fit) | |
.foregroundColor(self.isSelected ? Color.blue : Color.gray) | |
.frame(width: 28, height: 28) | |
.padding(.vertical) | |
} | |
} | |
struct TabViewWrapper: View { | |
let items: [TabViewWrapper.Tab] | |
@State var selectedIndex: Int = 0 | |
private func setSelectedIndex(_ selectedIndex: Int) { | |
self.selectedIndex = selectedIndex | |
} | |
var body: some View { | |
GeometryReader { geometry in | |
NavigationView { | |
VStack { | |
TabView(selection: self.$selectedIndex) { | |
ForEach(self.items.indices) { index in | |
self.items[index].presenter.tag(index) | |
} | |
}.tabBarHidden(true) | |
HStack(spacing: 0) { | |
ForEach(self.items.indices) { index in | |
TabButton( | |
icon: self.items[index].icon, | |
isSelected: self.selectedIndex == index | |
).frame(maxWidth: geometry.size.width / CGFloat(self.items.count)) | |
.onTapGesture { self.setSelectedIndex(index) } | |
} | |
} | |
}.background(Color(.clear).edgesIgnoringSafeArea(.bottom)) | |
} | |
} | |
} | |
struct Tab { | |
let presenter: AnyView | |
let icon: String | |
init<Presenter: View>(presenter: () -> Presenter, icon: String) { | |
self.presenter = AnyView(presenter()) | |
self.icon = icon | |
} | |
} | |
} | |
#if DEBUG | |
struct TabViewWrapper_Previews: PreviewProvider { | |
static var previews: some View { | |
TabViewWrapper(items: [ | |
TabViewWrapper.Tab(presenter: { Text("HouseView") }, icon: "house"), | |
TabViewWrapper.Tab(presenter: { Text("GearView") }, icon: "gear"), | |
TabViewWrapper.Tab(presenter: { Text("PhoneView") }, icon: "phone"), | |
TabViewWrapper.Tab(presenter: { Text("PinView") }, icon: "pin"), | |
TabViewWrapper.Tab(presenter: { Text("MapView") }, icon: "map"), | |
]) | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment