I am trying to draw a shape using swift, and here my code
struct ElementView: View {
var element: Element
var body: some View {
let rec = RoundedRectangle(cornerRadius: 25.0)
return ZStack {
rec
.frame(width: 300, height: 150, alignment: .center)
.foregroundColor(.pink)
.opacity(0.4)
.overlay(
rec.stroke(
Color.pink,
style: StrokeStyle(
lineWidth: 5,
lineCap: .round,
lineJoin: .round
)
)
)
Text("Text")
}
}
}
There is only one border, but I want two or more
My result: https://i.stack.imgur.com/n26Vb.png
What is expected: https://i.stack.imgur.com/L23cW.png
How can I add multiple borders?
Another approach with less code!
struct ElementView: View {
var body: some View {
Text("Text")
.frame(width: 300, height: 150, alignment: .center)
.background(Color.yellow.opacity(0.4))
.cornerRadius(15.0)
.overlay(RoundedRectangle(cornerRadius: 15.0).strokeBorder(Color.blue, lineWidth: 10.0))
.padding(10.0)
.overlay(RoundedRectangle(cornerRadius: 25.0).strokeBorder(Color.red, lineWidth: 10.0))
}
}
Related
I have a RoundedRectangle and I want to make the trim rotate forever and make a nice animation from it.
Tried to make a rotation effect and 3d rotation effect, but it didn't work
#State private var show = false
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 20, style: .continuous)
.foregroundColor(.gray.opacity(0.1))
.frame(width: 195, height: 50)
RoundedRectangle(cornerRadius: 20, style: .continuous)
.trim(from: 0.25, to: 0.5)
.stroke(style: StrokeStyle(lineWidth: 3, lineCap: .round))
.frame(width: show ? 50 : 200, height: show ? 200 : 50)
.rotationEffect(Angle(degrees: show ? 90 : 0))
.animation(.linear.delay(1).repeatForever(autoreverses: true),
value: show)
RoundedRectangle(cornerRadius: 20, style: .continuous)
.trim(from: 0.75, to: 1)
.stroke(style: StrokeStyle(lineWidth: 3, lineCap: .round))
.frame(width: show ? 50 : 200, height: show ? 200 : 50)
.rotationEffect(Angle(degrees: show ? 90 : 0))
.animation(.linear.delay(1).repeatForever(autoreverses: true),
value: show)
Text("title")
.foregroundStyle(LinearGradient(gradient: Gradient(colors: colors),
startPoint: .topLeading,
endPoint: .bottomTrailing))
}
.onAppear {
show.toggle()
}
THis is my code. Made it move, but not the desired way... could be smoother and like two snakes going one after the other one
Animate the changing of the trim offsets. Since it is difficult to animate past the boundaries of 0 and 1, use .rotation(Angle(degrees: 180)) to always stay within the boundaries of 0 and 1:
struct ContentView: View {
let colors = [Color.green, Color.blue]
#State private var offset = 0.0
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 20, style: .continuous)
.foregroundColor(.gray.opacity(0.1))
.frame(width: 195, height: 50)
RoundedRectangle(cornerRadius: 20, style: .continuous)
.trim(from: 0.25 + offset, to: 0.5 + offset)
.stroke(style: StrokeStyle(lineWidth: 3, lineCap: .round))
.frame(width: 195, height: 50)
RoundedRectangle(cornerRadius: 20, style: .continuous)
.trim(from: 0.25 + offset, to: 0.5 + offset)
.stroke(style: StrokeStyle(lineWidth: 3, lineCap: .round))
.rotation(Angle(degrees: 180))
.frame(width: 195, height: 50)
Text("title")
.foregroundStyle(LinearGradient(gradient: Gradient(colors: colors),
startPoint: .topLeading,
endPoint: .bottomTrailing))
}
.onAppear {
withAnimation(.linear(duration: 0.75).repeatForever(autoreverses: false)) {
offset = 0.5
}
}
}
}
Optimizing the code
Since the two "snakes" are identical except for the 180º rotation, we can code this with a ForEach:
ForEach([0.0, 180.0], id: \.self) { rotation in
RoundedRectangle(cornerRadius: 20, style: .continuous)
.trim(from: 0.25 + offset, to: 0.5 + offset)
.stroke(style: StrokeStyle(lineWidth: 3, lineCap: .round))
.rotation(Angle(degrees: rotation))
.frame(width: 195, height: 50)
}
when I use the textfield in my View, the Content in the vstack or scrollview is seen behind the custom navigation bar. How can I fix this background problem?
Code from the Current View
import SwiftUI
import MapKit
struct address_edit_address: View {
#State private var street: String = ""
#State private var titel: String = ""
#State private var comment: String = ""
#State var topLeft: CGFloat = 0
#State var topRight: CGFloat = 50
#State var bottomLeft: CGFloat = 50
#State var bottomRight: CGFloat = 0
#State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
var body: some View {
VStack{
VStack(alignment: .leading, spacing: 20){
Map(coordinateRegion: $region).frame(minWidth: 0, idealWidth: 383, maxWidth: 383, minHeight: 0, idealHeight: 128, maxHeight: 128).cornerRadius(10)
VStack (alignment: .leading, spacing: 10){
Text("Adresse:").font(.custom("Arboria-Medium", size:13))
HStack (alignment: .center){
TextField("Straße und Hausnummer*", text: $street).frame(minWidth: 0, idealWidth: 383, maxWidth: 383, minHeight: 0, idealHeight: 54, maxHeight: 54, alignment: .center)
.foregroundColor(Color("white"))
.background(RoundedRectangle(cornerRadius:10).fill(Color("grey_dark1_bg_obj")))
.overlay(
RoundedRectangle(cornerRadius:10).stroke(Color("grey_light2_obj_font"), lineWidth: 1))
}
}
VStack (alignment: .leading, spacing: 10){
Text("Adresstitel & Typ(optional):").font(.custom("Arboria-Medium", size:13))
HStack(spacing: 0){
TextField("Titel", text: $titel).frame(minWidth: 0, idealWidth: 230, maxWidth: 230, minHeight: 0, idealHeight: 54, maxHeight: 54, alignment: .leading)
.foregroundColor(Color("white"))
.background(RoundedCornerShape(radius:10, corners: [.topLeft, .bottomLeft]).fill(Color("grey_dark1_bg_obj")))
.overlay(
RoundedCornerShape(radius: 10, corners: [.topLeft, .bottomLeft]).stroke(Color("grey_light2_obj_font"), lineWidth: 1))
HStack(spacing: 0){
Button(action: {
print("Privat")
}){ VStack {
Image(systemName: "house.fill")
Text("Privat").font(.custom("Arboria-Medium", size:12))
}
}.frame(width: 74, height: 56).background(Color("blue")).border(Color("grey_light2_obj_font"), width: 1)
Button(action: {
print("Arbeit")
}){ VStack{
Image(systemName: "briefcase.fill")
Text("Arbeit").font(.custom("Arboria-Medium", size:12))
}
}.frame(width: 74, height: 54)
.background(RoundedCornerShape(radius: 10, corners: [.topRight, .bottomRight]).fill(Color("grey_dark1_bg_obj")))
.overlay(
RoundedCornerShape(radius: 10, corners: [.topRight, .bottomRight]).stroke(Color("grey_light2_obj_font"), lineWidth: 1))
}.frame(width: 148, height: 51, alignment: .leading)
}
}
VStack (alignment: .leading, spacing: 10){
Text("Anmerkung hinzufügen(optional):").font(.custom("Arboria-Medium", size:13))
HStack{
TextField("Anmerkung zur Lieferung", text: $comment).frame(minWidth: 0, idealWidth: 383, maxWidth: 383, minHeight: 0, idealHeight: 102, maxHeight: 102, alignment: .center)
.foregroundColor(Color("white"))
.background(RoundedRectangle(cornerRadius:10).fill(Color("grey_dark1_bg_obj")))
.overlay(
RoundedRectangle(cornerRadius:10).stroke(Color("grey_light2_obj_font"), lineWidth: 1))
}
HStack{
Text("*Pflichtfelder")
Spacer()
Text("0/180")
}.font(.custom("Arboria-Medium", size:13))
}.frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight - 600, alignment: .top)
}
VStack(spacing: 30){
HStack(spacing: 20){
Button("Speichern"){
}.frame(minWidth: 0, idealWidth: 160, maxWidth: 160, minHeight: 0, idealHeight: 43, maxHeight: 43, alignment: .center).background(Color("green_mid")).clipShape(RoundedRectangle(cornerRadius: 10)).font(.custom("Arboria-Medium", size: 14))
Button (action: {
print("Favorit")
}){ HStack {
Text("Favorisieren").font(.custom("Arboria-Medium", size: 14))
Image(systemName:"heart")
}.foregroundColor(Color("yellow"))
}.frame(minWidth: 0, idealWidth: 160, maxWidth: 160, minHeight: 0, idealHeight: 43, maxHeight: 43, alignment: .center).overlay(
RoundedCornerShape(radius: 10, corners: [.topRight, .bottomRight, .topLeft, .bottomLeft]).stroke(Color("yellow"), lineWidth: 1))
}
Button("Adresse entfernen"){
}.foregroundColor(Color("red")).font(.custom("Arboria-Medium", size: 14))
}
Spacer()
}.frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight).background(Color.black) .foregroundColor(Color.white)
}
}
struct address_edit_address_Previews: PreviewProvider {
static var previews: some View {
address_edit_address().preferredColorScheme(.dark)
}
}
Code from the template, where the navigation bar is implemented
import SwiftUI
struct account_template<Content: View>: View {
#Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
#ViewBuilder var content: Content
var title: String
var scrollable: Bool
init(content:Content, title:String, scrollable: Bool){
self.content = content
self.title = title
self.scrollable = scrollable
}
var btnBack : some View {
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}){
VStack{
ZStack{
HStack(){
Image(systemName: "arrow.left")
.aspectRatio(contentMode: .fit)
.foregroundColor(.white)
Spacer()
}.frame(width: UIScreen.screenWidth)
HStack (alignment:.center){
Text(title).foregroundColor(.white).font(.custom("Arboria-Medium", size:16))
}.frame(width: UIScreen.screenWidth)
}.frame(width: UIScreen.screenWidth, height: 50).background(Color("black"))
Divider().background(Color("white")).frame(height:2)
}.frame(height: 75, alignment: .center).background(Color.black).edgesIgnoringSafeArea(.top)
}
}
var body: some View {
VStack {
Spacer()
Divider().background(Color("white")).frame(height:2)
if scrollable {
ScrollView(showsIndicators:false){
content
}.background(Color.black) .foregroundColor(Color.white) .frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight - 75, alignment: .top)
}else{
VStack{
content
}.background(Color.black) .foregroundColor(Color.white) .frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight - 75, alignment: .top)
}
}.background(Color.black) .foregroundColor(Color.white) .frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight, alignment: .center).navigationBarBackButtonHidden(true)
.navigationBarItems(trailing: btnBack)
}
}
struct adress_template_Previews: PreviewProvider {
static var previews: some View {
account_template(content: address_add_address(), title: "Adresse hinzufügen", scrollable: true)
}
}
In SwiftUI I am adding buttons to an HStack. I want a space of a fixed width at the beginning of the HStack. Currently the buttons are directly up against the left edge of the screen and I want a space of 64px before the first button. I cannot seem to find an answer to this, my Google-Fu may be failing me.
Here is what I have:
struct MyView: View {
var body: some View {
HStack(alignment: .center, spacing: 12, content: {
Button(action: doThis) {
Image("this").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
Button(action: doThat) {
Image("that").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
Spacer()
})
}
func doThis() {}
func doThat() {}
}
Just add some padding on the left.
struct ContentView: View {
var body: some View {
HStack(alignment: .center, spacing: 12, content: {
Button(action: doThis) {
Image("this").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
.padding(.leading, 64) /// here!
Button(action: doThat) {
Image("that").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
Spacer()
})
}
func doThis() {}
func doThat() {}
}
Before
After
How can I add a button at this location in SwiftUI? I have a textfield and a background being used, but I'm not sure how you can add a button to this location, like a bar button in swift.
My code:
struct MyTextField: View
{
#ObservedObject var model: TextFieldModel
var body: some View
{
VStack(spacing: 0)
{
Color.black
.frame(height: 100)
.overlay(Image(("appIconLogo")).resizable().frame(width: 70, height: 70),
alignment: .bottom)
.edgesIgnoringSafeArea(.top)
VStack(alignment: .leading, spacing: 10)
{
Text("Welcome!")
.font(.system(size: 60, design: .rounded))
.foregroundColor(.black)
.padding(.leading)
iTextField("Search Your Ticker", text: $model.text, isEditing: $model.isEditing)
.foregroundColor(.black)
.showsClearButton(true)
.autocapitalization(.allCharacters)
.disableAutocorrection(true)
.returnKeyType(.search)
.onReturn
{
print($model.text)
NotificationCenter.default.post(name: Notification.Name(rawValue: "isValidTicker"), object: nil)
}
.foregroundColor(.black)
.style(height: 58, font: nil, paddingLeading: 25, cornerRadius: 6, hasShadow: true, image: Image(systemName: "magnifyingglass"))
.foregroundColor(.black)
}.frame(maxWidth: .infinity, alignment: .leading)
}.frame(maxHeight: .infinity, alignment: .top)
}
}
I assume you wanted something like this
Color.black
.frame(height: 100)
.overlay(Image(("appIconLogo")).resizable().frame(width: 70, height: 70), alignment: .bottom)
.overlay(Button("Perform") {}.padding(), alignment: .leading) // << here!!
.edgesIgnoringSafeArea(.top)
Any particular reason you have the Color.black with an overlay for the logo? Using your code, wrapping it in a ZStack should work
ZStack(alignment: .leading) {
Color.black
.frame(height: 100)
.overlay(Image(("appIconLogo")).resizable().frame(width: 70, height: 70), alignment: .bottom)
.edgesIgnoringSafeArea(.top)
Button(action:{}) {
Text("Button Text")
}
}
I want to implement a SwiftUI neomorphic button in SwiftUI but the shadows are messed up. To fix it, I use blendmode(overlay), but it has no effect on the button in NavBar. It works fine on the button in the regular view. The problem
Here’s my code
struct ContentView: View {
var body: some View {
NavigationView{
Button("Hi") { }
.foregroundColor(Color.primary)
.padding()
.background(
ZStack {
RoundedRectangle(cornerRadius: 12, style: .continuous)
.shadow(
color: .white,
radius: 12,
x: -6,
y: -8
)
.shadow(
color: Color.black.opacity(0.8),
radius: 12,
x: 6,
y: 8
)
.blendMode(.overlay)
.padding(2)
RoundedRectangle(cornerRadius: 12, style: .continuous)
.foregroundColor(color)
}
)
.navigationBarColor(UIbackground)
.navigationBarTitle("SwiftUI")
.frame(minWidth: 0, maxWidth: .infinity,
minHeight: 0, maxHeight: .infinity,
alignment: .center)
.background(color)
.navigationBarItems(leading: Button("Hi") { }
.foregroundColor(Color.primary)
.padding()
.background(
ZStack {
RoundedRectangle(cornerRadius: 12, style: .continuous)
.shadow(
color: .white,
radius: 12,
x: -6,
y: -8
)
.shadow(
color: Color.black.opacity(0.8),
radius: 12,
x: 6,
y: 8
)
.blendMode(.overlay)
.padding(2)
RoundedRectangle(cornerRadius: 12, style: .continuous)
.foregroundColor(color)
}
))
}
}
}
Any help would be appreciated.