Advertisement
Guest User

Sheet view issues

a guest
Jun 1st, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.67 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. struct ContentView: View {
  4.     @State var sheetIsPresented: Bool = false
  5.     @State var count: Int = 0
  6.    
  7.     @State var color: Color = .white
  8.    
  9.     var body: some View {
  10.         ZStack {
  11.             color
  12.                 .ignoresSafeArea()
  13.             VStack {
  14.                 HStack {
  15.                     Text(String(count))
  16.                     Button("Count +1") {
  17.                         count += 1
  18.                     }
  19.                 }
  20.                 Button("Show sheet") {
  21.                     sheetIsPresented = true
  22.                 }
  23.             }
  24.             .padding()
  25.             .background(Color(.white).shadow(radius: 2))
  26.            
  27.         }
  28.         .sheet(isPresented: $sheetIsPresented, onDismiss: {
  29.             color = .random
  30.         }) {
  31.             VStack {
  32.                 Button("isPresented = false") {
  33.                     sheetIsPresented = false
  34.                 }
  35.                 SheetView()
  36.                     .presentationDetents([.fraction(0.49)])
  37.                     .presentationBackgroundInteraction(.enabled)
  38.                     .presentationDragIndicator(.visible)
  39.                 Spacer()
  40.             }
  41.             .padding(.top, 25)
  42.         }
  43.     }
  44. }
  45.  
  46. struct SheetView: View {
  47.     @Environment(\.dismiss) var dismiss
  48.     var body: some View {
  49.         Button("Dismiss via dismiss()") {
  50.             dismiss()
  51.         }
  52.     }
  53. }
  54.  
  55. /// Extension to generate random colors
  56. extension Color {
  57.     static var random: Color {
  58.         return Color(hue: Double.random(in: 0...1), saturation: Double.random(in: 0...1), brightness: Double.random(in: 0...1))
  59.     }
  60. }
  61.  
  62. #Preview {
  63.     ContentView()
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement