-
-
Save ryanlintott/f6a06379a1f0b99e75672469ced9f3c6 to your computer and use it in GitHub Desktop.
Image that can be revealed between other elements in a VStack.
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 | |
struct ContentView: View { | |
@State private var pictureExpanded = false | |
var body: some View { | |
VStack(alignment: .leading, spacing: 20.0) { | |
Text("Once upon a time there was a turtle named George who made friends with a giraffe at the local water park and then they went on lots of adventures together.") | |
Button { | |
withAnimation { | |
pictureExpanded.toggle() | |
} | |
} label: { | |
Text("Tap to \(pictureExpanded ? "hide" : "see") a pretty picture") | |
} | |
Image("cat") | |
.resizable() // Allows resizing | |
.scaledToFit() // Scales to fit available space keeping image aspect ratio | |
.fixedSize(horizontal: false, vertical: true) // ignores height restrictions but not width | |
.frame(height: pictureExpanded ? nil : 0) // Defaults to image height when expanded and limits to zero when not. | |
.contentShape(Rectangle()) // Changes hit area to match new frame (otherwise image will block button) | |
.clipped() // Clips content to match new frame (otherwise image will show when frame is zero height) | |
Text("The giraffe's name was Leonard, and together George and Leonard became the best of friends and did all sorts of cool things together like climbing a mountain.") | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment