Guest User

Untitled

a guest
Dec 14th, 2025
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.96 KB | Source Code | 0 0
  1. import SwiftUI
  2.  
  3. enum ToolbarType {
  4.     case none, toggle, timer, compass, color, search
  5. }
  6.  
  7. struct FancyToolbarView: View {
  8.     @State private var toolbarType: ToolbarType = .none
  9.  
  10.     var body: some View {
  11.         NavigationStack {
  12.             Rectangle()
  13.                 .fill(Color.blue.gradient)
  14.                 .ignoresSafeArea()
  15.                 .overlay {
  16.                     Rectangle()
  17.                         .fill(.black.opacity(0.6))
  18.                         .ignoresSafeArea()
  19.                 }
  20.                 .toolbar {
  21.                     toolbarContent
  22.                 }
  23.         }
  24.     }
  25.  
  26.     @ToolbarContentBuilder
  27.     private var toolbarContent: some ToolbarContent {
  28.         switch toolbarType {
  29.         case .none:
  30.             BottomBarButton("Switch", systemImage: "switch.2") {}
  31.             BottomBarSpacer()
  32.             BottomBarButton("Timer", systemImage: "clock") {
  33.                 withAnimation { toolbarType = .timer }
  34.             }
  35.             BottomBarSpacer()
  36.             BottomBarButton("Compass", systemImage: "arrowtriangle.backward.fill") {}
  37.             BottomBarSpacer()
  38.             BottomBarButton("Contrast", systemImage: "circle.hexagongrid") {}
  39.             BottomBarSpacer()
  40.             BottomBarButton("Search", systemImage: "magnifyingglass") {}
  41.  
  42.         case .timer:
  43.             ToolbarItem(placement: .bottomBar) {
  44.                 HStack {
  45.                     Image(systemName: "xmark")
  46.                         .padding(8)
  47.                         .onTapGesture {
  48.                             withAnimation { toolbarType = .none }
  49.                         }
  50.  
  51.                     ScrollView(.horizontal) {
  52.                         HStack {
  53.                             ForEach(1..<50) {
  54.                                 Text("\($0)")
  55.                             }
  56.                         }
  57.                     }
  58.                     .scrollIndicators(.hidden)
  59.                 }
  60.                 .frame(maxWidth: .infinity)
  61.             }
  62.             BottomBarSpacer()
  63.             BottomBarButton("Search", systemImage: "magnifyingglass") {}
  64.  
  65.         default:
  66.             BottomBarSpacer()
  67.             BottomBarButton("Search", systemImage: "magnifyingglass") {}
  68.         }
  69.        
  70.        
  71.     }
  72. }
  73.  
  74. struct BottomBarSpacer: ToolbarContent {
  75.     var body: some ToolbarContent {
  76.         ToolbarItem(placement: .bottomBar) {
  77.             Spacer()
  78.         }
  79.     }
  80. }
  81.  
  82. struct BottomBarButton: ToolbarContent {
  83.     let title: String
  84.     let systemImage: String
  85.     let action: () -> Void
  86.  
  87.     init(
  88.         _ title: String,
  89.         systemImage: String,
  90.         action: @escaping () -> Void
  91.     ) {
  92.         self.title = title
  93.         self.systemImage = systemImage
  94.         self.action = action
  95.     }
  96.  
  97.     var body: some ToolbarContent {
  98.         ToolbarItem(placement: .bottomBar) {
  99.             Button(title, systemImage: systemImage, action: action)
  100.         }
  101.     }
  102. }
  103.  
  104. #Preview {
  105.     FancyToolbarView()
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment