Created
July 13, 2023 14:26
-
-
Save Codelaby/f383545a0b9a3bd8ca1c4b7dc38fc4bc to your computer and use it in GitHub Desktop.
Adaptative Layout Swift
This file contains hidden or 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
struct AdaptLayout: View { | |
var body: some View { | |
VStack { | |
sampleTitle("View That Fits") | |
Spacer() | |
ViewThatFits(in: .horizontal) { | |
ViewHorizontal() | |
ViewVertical() | |
} | |
Spacer() | |
credits | |
} | |
} | |
@ViewBuilder | |
func ViewVertical() -> some View { | |
VStack { | |
Text("00:00:00") | |
.font(.system(size: 68)) | |
.padding() | |
HStack { | |
Button(action: { | |
// Do something when the previous button is tapped. | |
}) { | |
Image(systemName: "arrow.left") | |
} | |
.buttonStyle(.bordered) | |
Button(action: { | |
// Do something when the play button is tapped. | |
}) { | |
Image(systemName: "play.fill") | |
} | |
.buttonStyle(.bordered) | |
Button(action: { | |
// Do something when the next button is tapped. | |
}) { | |
Image(systemName: "arrow.right") | |
} | |
.buttonStyle(.bordered) | |
} | |
} | |
.padding() | |
.border(.red) | |
} | |
@ViewBuilder | |
func ViewHorizontal() -> some View { | |
HStack { | |
Text("00:00:00") | |
.font(.system(size: 68)) | |
.padding() | |
HStack { | |
Button(action: { | |
// Do something when the previous button is tapped. | |
}) { | |
Image(systemName: "arrow.left") | |
} | |
.buttonStyle(.bordered) | |
Button(action: { | |
// Do something when the play button is tapped. | |
}) { | |
Image(systemName: "play.fill") | |
} | |
.buttonStyle(.bordered) | |
Button(action: { | |
// Do something when the next button is tapped. | |
}) { | |
Image(systemName: "arrow.right") | |
} | |
.buttonStyle(.bordered) | |
} | |
} | |
.padding() | |
.border(.blue) | |
} | |
@ViewBuilder | |
func sampleTitle(_ title: String = "sample") -> some View { | |
VStack(spacing: 8) { | |
Text(title) | |
.font(.title) | |
Label("XCODE 15 - IOS16", | |
systemImage: "swift").font(.caption2).foregroundStyle(.secondary) | |
} | |
.padding() | |
.background(.thinMaterial, in: RoundedRectangle(cornerRadius: 20)) | |
} | |
@ViewBuilder | |
var credits: some View { | |
VStack() { | |
Text("by @Codelaby") | |
.font(.callout).foregroundStyle(.secondary) | |
Text("bento.me/codelaby").font(.callout).foregroundStyle(.teal) | |
} | |
} | |
} | |
struct Memorize_Previews: PreviewProvider { | |
static var previews: some View { | |
AdaptLayout() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment