Skip to content

Instantly share code, notes, and snippets.

@DaisukeNagata
Last active January 14, 2020 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaisukeNagata/e64c61d4a407ef62797079eebfd4ff03 to your computer and use it in GitHub Desktop.
Save DaisukeNagata/e64c61d4a407ef62797079eebfd4ff03 to your computer and use it in GitHub Desktop.
SwiftUI_CustomTabBar
import SwiftUI
class ViewRouter: ObservableObject { @Published var iconName = "arrow.up.right.video.fill" }
struct ContentView: View {
@ObservedObject var viewRouter = ViewRouter()
@State private var selection: Int = 1
var body: some View {
GeometryReader { geometry in
VStack {
Spacer()
Text(self.viewRouter.iconName)
Spacer()
HStack {
self.modifier(TabModifier(text: "arrow.up.right.video.fill", selection: self.$selection, viewRouter: self.viewRouter, tag: 1))
self.modifier(TabModifier(text: "icloud.and.arrow.up.fill", selection: self.$selection,viewRouter: self.viewRouter, tag: 2))
self.modifier(TabModifier(text: "lock.icloud.fill", selection: self.$selection, viewRouter: self.viewRouter, tag: 3))
self.modifier(TabModifier(text: "message.fill", selection: self.$selection,viewRouter: self.viewRouter, tag: 4))
}
.frame(width: geometry.size.width, height: geometry.size.height/10)
.background(Color.white.shadow(radius: 2)).offset(x: 0, y: -10)
}.edgesIgnoringSafeArea(.bottom).offset(x: 0, y: 10)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct TabModifier: ViewModifier {
var text: String? = ""
let selection: Binding<Int>
var viewRouter: ViewRouter?
var tag: Int
var width = UIScreen.main.bounds.width
func body(content: Content) -> some View {
return GeometryReader { geometry in
HStack {
Image(systemName: self.text ?? "")
.resizable()
.aspectRatio(contentMode: .fit)
.padding(20)
.frame(width: geometry.size.width, height: self.width/4 - 20)
.foregroundColor(self.fgColor())
.onTapGesture {
self.selection.wrappedValue = self.tag
self.viewRouter?.iconName = self.text ?? ""
}
}
}
}
private func fgColor() -> Color {
return selection.wrappedValue == self.tag ? Color(UIColor.systemBlue) : Color(UIColor.systemGray)
}
}
@DaisukeNagata
Copy link
Author

GifMovie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment