Flexibler Höhenblatt für SwiftuiIOS

Programmierung für iOS
Anonymous
 Flexibler Höhenblatt für Swiftui

Post by Anonymous »

Ich arbeite an einem Swiftui -Blatt, das sich anhand der Höhe des Inhalts an die Größe des Inhalts ändern kann. Das Blatt funktioniert gut, bis ich Multi-Line-Text einführe. Wenn ich dies jedoch tue, bemerke ich ein seltsames Animationsverhalten mit der Schaltfläche. Es springt auf, wenn Sie darauf tippen. Hier ist mein Code.

Code: Select all

struct SheetHeightModifier: ViewModifier {
@Binding var height: CGFloat

func body(content: Content) -> some View {
content
.fixedSize(horizontal: false, vertical: true)
.background(
GeometryReader { reader -> Color in
height = reader.size.height
print(height)
return Color.clear
}
)
}
}

struct PresentationDetentModifier: ViewModifier {
@Binding var height: CGFloat

func body(content: Content) -> some View {
content
.modifier(SheetHeightModifier(height: $height))
.presentationDetents([.height(height)])
}
}

extension View {
func flexiblePresentationDetents(height: Binding) -> some View {
self.modifier(PresentationDetentModifier(height: height))
}
}

struct ContentView: View {
@State var showSheet = false
@State var sheetHeight: CGFloat = 0

var body: some View {
VStack {
Button("Present Sheet") {
showSheet = true
}
}
.sheet(isPresented: $showSheet) {
VStack {
Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
}
.padding()
.flexiblePresentationDetents(height: $sheetHeight)
}
.padding()
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post