Skip to content

Instantly share code, notes, and snippets.

@SoundBlaster
Last active September 29, 2023 18:54
Show Gist options
  • Save SoundBlaster/8a11e267e4953131d787dcc60726f903 to your computer and use it in GitHub Desktop.
Save SoundBlaster/8a11e267e4953131d787dcc60726f903 to your computer and use it in GitHub Desktop.
SwiftUI Rectangle with outside border and inner gradient shadow
import SwiftUI
import PlaygroundSupport
struct RoundedRect: View {
var body: some View {
RoundedRectangle(cornerRadius: 8, style: .continuous)
.fill(.blue.opacity(0))
.overlay(
ZStack {
RoundedRectangle(cornerRadius: 8, style: .continuous)
.stroke(.black.opacity(0.15), lineWidth: 4)
RoundedRectangle(cornerRadius: 8, style: .continuous)
.fill(.blue.gradient)
RoundedRectangle(cornerRadius: 8, style: .continuous)
.strokeBorder(
LinearGradient(stops: [Gradient.Stop(color: .white, location:0.0),
Gradient.Stop(color: .white.opacity(0.0), location:0.25),
Gradient.Stop(color: .white.opacity(0.0), location:1.0)],
startPoint: UnitPoint(x: 0, y: 0),
endPoint: UnitPoint(x: 0, y: 1)).opacity(0.15),
lineWidth: 2
)
}
)
}
}
struct ContentView: View {
var body: some View {
HStack {
Button {
print("tap")
} label: {
Text("Tap me!")
.padding()
.foregroundColor(.white)
.background(
RoundedRect()
)
.frame(width: 200, height: 66)
}
.padding()
}
.background(.gray)
}
}
PlaygroundPage.current.setLiveView(ContentView())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment