Last active
January 14, 2020 12:56
-
-
Save DaisukeNagata/e64c61d4a407ef62797079eebfd4ff03 to your computer and use it in GitHub Desktop.
SwiftUI_CustomTabBar
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
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) | |
} | |
} |
Author
DaisukeNagata
commented
Jan 13, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment