Last active
March 12, 2020 23:24
-
-
Save DaisukeNagata/3e5bc68b8d2cca5ac7d1682b7ef1ad9a to your computer and use it in GitHub Desktop.
SwiftUI_MyModifier_ButtonSegue
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 { | |
GeometryReader { geometry in | |
NavigationView { | |
ZStack { | |
VStack { | |
Text("Something") | |
.modifier(MyModifier(color: .yellow, | |
frameSizeHeight: 40, | |
frameSizeWidth: 100, // frmae is size | |
verticalSet: .top, // originY | |
horizontalSet: .none, // originX | |
verticalSize: -80, // originY size | |
horizontalSize: 0)) | |
Text("Something").modifier( | |
MyModifier(color: .yellow, | |
frameSizeHeight: UIScreen.main.bounds.height, | |
frameSizeWidth: UIScreen.main.bounds.width, | |
verticalSet: .none, | |
horizontalSet: .none, | |
verticalSize: 0, | |
horizontalSize: 0)) | |
.padding(.horizontal) | |
} | |
CommonButton(0) | |
} | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
struct SecondView: View { | |
var body: some View { | |
Text("画面遷移できました") | |
} | |
} | |
struct MyModifier: ViewModifier { | |
var color : Color? = nil | |
var frameSizeHeight : CGFloat? = nil | |
var frameSizeWidth : CGFloat? = nil | |
var frameSize : CGFloat? = nil | |
var verticalSet : Edge.Set? = nil | |
var horizontalSet : Edge.Set? = nil | |
var verticalSize : CGFloat? = nil | |
var horizontalSize : CGFloat? = nil | |
func body(content: Content) -> some View { | |
return content | |
.frame(maxWidth: frameSizeWidth, maxHeight: frameSizeHeight) | |
.background(color) | |
.padding(verticalSet ?? Edge.Set.init(rawValue: 0), verticalSize ?? 0.0) | |
.padding(horizontalSet ?? Edge.Set.init(rawValue: 0), horizontalSize ?? 0.0) | |
} | |
} | |
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(SecondView()) | |
} | |
}) { | |
Text("次へ") | |
.frame(minWidth: 0, maxWidth: .infinity, maxHeight: 64) | |
.foregroundColor(Color.red) | |
} | |
.background(Color.gray) | |
.padding(.horizontal) | |
NavigationLink(destination: self.destination, tag: self.tag ?? 0, selection: $tag) { | |
EmptyView() | |
} | |
} | |
} | |
} |
Author
DaisukeNagata
commented
Mar 12, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment