Created
March 7, 2020 14:25
-
-
Save DaisukeNagata/d010ece5bacd8416119f48d65308a6c4 to your computer and use it in GitHub Desktop.
SwiftUI_NavigationView
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 { | |
var body: some View { | |
NavigationView { | |
VStack { | |
Text("Hello, World!") | |
CommonButton(0) | |
} | |
.background(Color.blue) | |
} | |
} | |
} | |
struct ContentView2: View { | |
var body: some View { | |
VStack { | |
Text("Hello, World!2") | |
.background(Color.red) | |
.foregroundColor(Color.white) | |
CommonButton(1) | |
.padding(.bottom) | |
.padding(.bottom) | |
} | |
.background(Color.purple) | |
.edgesIgnoringSafeArea(.bottom) | |
} | |
} | |
struct ContentView3: View { | |
var body: some View { | |
VStack { | |
Group { | |
Spacer() | |
Spacer() | |
Spacer() | |
Spacer() | |
Spacer() | |
Spacer() | |
} | |
Group { | |
Spacer() | |
Spacer() | |
Spacer() | |
Spacer() | |
Spacer() | |
Spacer() | |
} | |
Group { | |
Spacer() | |
Spacer() | |
Spacer() | |
Spacer() | |
Spacer() | |
Spacer() | |
} | |
Text("Hello, World!3") | |
CommonButton(2) | |
.padding(.bottom) | |
.padding(.bottom) | |
} | |
.edgesIgnoringSafeArea(.all) | |
} | |
} | |
struct ContentView4: View { | |
var body: some View { | |
VStack { | |
ContentSpace() | |
Text("Hello, World!4") | |
ContentSpace() | |
} | |
.edgesIgnoringSafeArea(.all) | |
} | |
} | |
struct ContentSpace: View { | |
var body: some View { | |
VStack { | |
Group { | |
Spacer() | |
Spacer() | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
struct CommonButton: View { | |
var id: Int? | |
@State var tag: Int? | |
@State var destination: AnyView? | |
init(_ id: Int) { | |
self.id = id | |
} | |
var body: some View { | |
VStack { | |
Spacer() | |
Button(action: { | |
if self.id == 0 { | |
self.tag = 0 | |
self.destination = AnyView(ContentView2()) | |
} else if self.id == 1 { | |
self.tag = 1 | |
self.destination = AnyView(ContentView3()) | |
} else if self.id == 2 { | |
self.tag = 2 | |
self.destination = AnyView(ContentView4()) | |
} | |
}) { | |
Text("次へ") | |
.frame(minWidth: 0, maxWidth: .infinity) | |
.foregroundColor(Color.red) | |
} | |
.background(Color.gray) | |
.padding(.horizontal) | |
NavigationLink(destination: self.destination, tag: self.tag ?? 0, selection: $tag) { | |
EmptyView() | |
} | |
} | |
} | |
} |
Author
DaisukeNagata
commented
Mar 7, 2020
X系以外は
https://gist.github.com/daisukenagata/3897782246db07fb7b64b8e791ddb842
safeareaをヒントにしてください。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment