Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SwiftUI
- enum ToolbarType {
- case none, toggle, timer, compass, color, search
- }
- struct FancyToolbarView: View {
- @State private var toolbarType: ToolbarType = .none
- var body: some View {
- NavigationStack {
- Rectangle()
- .fill(Color.blue.gradient)
- .ignoresSafeArea()
- .overlay {
- Rectangle()
- .fill(.black.opacity(0.6))
- .ignoresSafeArea()
- }
- .toolbar {
- toolbarContent
- }
- }
- }
- @ToolbarContentBuilder
- private var toolbarContent: some ToolbarContent {
- switch toolbarType {
- case .none:
- BottomBarButton("Switch", systemImage: "switch.2") {}
- BottomBarSpacer()
- BottomBarButton("Timer", systemImage: "clock") {
- withAnimation { toolbarType = .timer }
- }
- BottomBarSpacer()
- BottomBarButton("Compass", systemImage: "arrowtriangle.backward.fill") {}
- BottomBarSpacer()
- BottomBarButton("Contrast", systemImage: "circle.hexagongrid") {}
- BottomBarSpacer()
- BottomBarButton("Search", systemImage: "magnifyingglass") {}
- case .timer:
- ToolbarItem(placement: .bottomBar) {
- HStack {
- Image(systemName: "xmark")
- .padding(8)
- .onTapGesture {
- withAnimation { toolbarType = .none }
- }
- ScrollView(.horizontal) {
- HStack {
- ForEach(1..<50) {
- Text("\($0)")
- }
- }
- }
- .scrollIndicators(.hidden)
- }
- .frame(maxWidth: .infinity)
- }
- BottomBarSpacer()
- BottomBarButton("Search", systemImage: "magnifyingglass") {}
- default:
- BottomBarSpacer()
- BottomBarButton("Search", systemImage: "magnifyingglass") {}
- }
- }
- }
- struct BottomBarSpacer: ToolbarContent {
- var body: some ToolbarContent {
- ToolbarItem(placement: .bottomBar) {
- Spacer()
- }
- }
- }
- struct BottomBarButton: ToolbarContent {
- let title: String
- let systemImage: String
- let action: () -> Void
- init(
- _ title: String,
- systemImage: String,
- action: @escaping () -> Void
- ) {
- self.title = title
- self.systemImage = systemImage
- self.action = action
- }
- var body: some ToolbarContent {
- ToolbarItem(placement: .bottomBar) {
- Button(title, systemImage: systemImage, action: action)
- }
- }
- }
- #Preview {
- FancyToolbarView()
- }
Advertisement
Add Comment
Please, Sign In to add comment