Last active
September 20, 2022 00:35
-
-
Save enomoto/a2b4453d015e9e054a9d0e73bf8c0227 to your computer and use it in GitHub Desktop.
Sample for simple modifier
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
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
VStack { | |
globeLabel | |
.padding() | |
.mainCell() // Applying quite simple custom modifier | |
} | |
} | |
private var globeLabel: some View { | |
Label("Hello, world!", systemImage: "globe") | |
} | |
} |
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
import SwiftUI | |
extension View { | |
func mainCell() -> some View { | |
background( | |
Color.cyan.opacity(0.1) | |
) | |
.cornerRadius(20) | |
} | |
} | |
// or implement like this ... | |
extension View { | |
func mainCell2() -> some View { | |
modifier(MainCellModifier()) | |
} | |
} | |
private struct MainCellModifier: ViewModifier { | |
func body(content: Content) -> some View { | |
content.background( | |
Color.cyan.opacity(0.1) | |
) | |
.cornerRadius(20) | |
} | |
} |
Author
enomoto
commented
Sep 20, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment