SwiftUI: apply background color to rounded rectangle - ios

I would like to create a button with a rounded rectangle view with a border, and a background color.
I wrote this so far :
Button(action: {
}, label: {
Text("TAP ME")
.foregroundColor(Color.blue)
})
.frame(height: 50)
.frame(maxWidth: .infinity)
.overlay(
RoundedRectangle(cornerRadius: 50, style: .continuous)
.strokeBorder(Color.blue, lineWidth: 1)
)
I tried to add .background(Color.red) at many different places, but here is what I get everytime:
What to do here?
Thank you for your help

As far as I understood your needs, here is a solution
Button(action: {
}, label: {
Text("TAP ME")
.foregroundColor(Color.blue)
})
.frame(height: 50)
.frame(maxWidth: .infinity)
.background(
RoundedRectangle(cornerRadius: 50, style: .continuous).fill(Color.red)
)
.overlay(
RoundedRectangle(cornerRadius: 50, style: .continuous)
.strokeBorder(Color.blue, lineWidth: 1)
)

Related

How do I make my logo into a button in swiftUI?

I'm trying to get an image of a logo to be a button. I'm new to Swift/SwiftUI and the resources I've found so far seem to be outdated information. I have the image loaded in to see that I can get the image in there, and I've created a button in the location I want it, I just want to combine the two. Code is below.
struct ContentView: View {
var body: some View {
VStack {
Image("logo1024")
.resizable()
.padding()
.frame(width: 120, height: 120)
Group{
Button(action: {
print("tapped!")
}, label: {
Text("+")
.foregroundColor(.white)
.frame(width: 40, height: 40)
.background(Color.green)
.cornerRadius(15)
.padding()
})
}.frame(maxHeight: .infinity, alignment: .bottom)
}
}
}
Just place image instead of Text into button (all other modifiers on your needs), like
Group{
Button(action: {
print("tapped!")
}, label: {
Image("logo1024")
.resizable()
.padding()
.frame(width: 120, height: 120)
.foregroundColor(.white)
.frame(width: 40, height: 40)
.background(Color.green)
.cornerRadius(15)
.padding()
})
}.frame(maxHeight: .infinity, alignment: .bottom)

SwiftUI View is taking exceeding edges

I am learning SwiftUI and was trying to replicate an app. I ran into a problem where the view is taking up space outside the frame as well.
It looks like this:
My code for the view is:
struct LessonsScreen: View {
var body: some View {
VStack (alignment: .leading) {
HStack {
Image(systemName: "arrow.left")
.font(.system(size: 30))
Spacer()
Image(systemName: "slider.horizontal.3")
.font(.system(size: 30))
}
Text("A2 - Elementary")
.font(.system(size: 28, weight: .semibold))
.padding()
LessonCompletion(lessonNum: 1, text: "How are you?", color: .purple)
Image("discussion")
.cornerRadius(20)
.frame(width: UIScreen.main.bounds.width, alignment: .center)
LessonCompletion(lessonNum: 2, text: "Pronunciation", color: .green)
LessonCompletion(lessonNum: 3, text: "Demonstrative pronouns", color: .red)
LessonCompletion(lessonNum: 4, text: "Present continuous", color: .yellow)
Button(action: {}, label: {
Text("Get started")
.foregroundColor(.white)
.frame(width: UIScreen.main.bounds.width - 150, height: 70, alignment: .center)
.background(Color.black)
.cornerRadius(10)
.frame(width: UIScreen.main.bounds.width, alignment: .center)
.padding()
})
}
}
}
Can anyone tell me where I messed up the formatting?
If you like to align the button in center in native SwiftUI way, you can use view modifier Spacer() inside HStack() instead of .frame, like this: (and same with 'discussion' Image)
...
Button(action: {}, label: {
HStack{
Spacer()
Text("Get started")
.foregroundColor(.white)
.frame(width: UIScreen.main.bounds.width - 150, height: 70, alignment: .center)
.background(Color.black)
.cornerRadius(10)
.padding()
Spacer()
}
})
...
In your button replace
.frame(width: UIScreen.main.bounds.width, alignment: .center)
with
.frame(maxWidth: .infinity, alignment: .center)

How to hide the overflow of the background colour when adding corner radius in SwiftUI?

I want to have a view with rounded corners and a custom background colour, but the latter overflows in the corners, as showed here:
This is my code:
var id:Int
var body: some View {
VStack(alignment: .leading) {
Text(name)
.font(.title2)
.bold()
.foregroundColor(.accentColor)
Text("Index numéro " + String(id))
.foregroundColor(.accentColor.opacity(0.75))
}
.padding()
.frame(width: UIScreen.width*0.9,
height: UIScreen.height*0.1,
alignment: .leading)
.overlay(RoundedRectangle(cornerRadius: 20)
.stroke(Color.accentColor, lineWidth: 3))
.background(Color.accentColor.opacity(0.1))
}
Thanks in advance, I hope you guys will help me solve this issue!
You can use a RoundedRectangle with strokeBorder and then a background of an additional RoundedRectangle with a fill modifier. This technique is detailed here: SwiftUI: How to draw filled and stroked shape?
var body: some View {
VStack(alignment: .leading) {
Text(name)
.font(.title2)
.bold()
.foregroundColor(.accentColor)
Text("Index numéro " + String(id))
.foregroundColor(.accentColor.opacity(0.75))
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding()
.background(
RoundedRectangle(cornerRadius: 20)
.strokeBorder(Color.accentColor, lineWidth: 3)
.background(
RoundedRectangle(cornerRadius: 20).fill(Color.accentColor.opacity(0.3))
)
)
.padding()
}
Note: I've also changed your .frame modifier and replaced it with padding, which generally would be a more flexible solution that doesn't rely on measuring the screen size, but it's inconsequential to this answer
Simply replace this code:
.background(Color.accentColor.opacity(0.1).cornerRadius(20.0))
In iOS 15 there is a .background() modifier clipped to shape.
func background<S, T>(_ style: S, in shape: T, fillStyle: FillStyle = FillStyle()) -> some View where S : ShapeStyle, T : InsettableShape
Try this:
struct ContentView: View {
var body: some View {
VStack(alignment: .leading) {
Text("name")
.font(.title2)
.bold()
.foregroundColor(.accentColor)
Text("Index numéro " + "1")
.foregroundColor(.accentColor.opacity(0.75))
}
.padding()
.frame(width: 200,
height: 100,
alignment: .leading)
.overlay(RoundedRectangle(cornerRadius: 20)
.stroke(Color.accentColor, lineWidth: 3))
.background(Color.accentColor.opacity(0.1), in: RoundedRectangle(cornerRadius: 20))
}
}

rounded rectangle as a background in swiftUI

I tried to do an overlay, but it doesn't work ,
That part on the bottom keeps bugging me.
is there any other method beside doing an overlay of a rectangle??
ZStack {
RoundedRectangle(cornerRadius: 15)
.foregroundColor(Color("Hello"))
.frame(height: 700, alignment: /*#START_MENU_TOKEN#*/.center/*#END_MENU_TOKEN#*/)
.overlay(
VStack {
RoundedRectangle(cornerRadius: 15)
.frame(width:381, height: 130.36, alignment: /*#START_MENU_TOKEN#*/.center/*#END_MENU_TOKEN#*/)
.shadow(radius: 4,y:3)
.foregroundColor(.white)
.overlay(
HStack {
Button(action: /*#START_MENU_TOKEN#*//*#PLACEHOLDER=Action#*/{}/*#END_MENU_TOKEN#*/) {
ButtonBig(ButtonText: "Keuangan",ButtonIcon: "creditcard.fill")
}
Button(action: /*#START_MENU_TOKEN#*//*#PLACEHOLDER=Action#*/{}/*#END_MENU_TOKEN#*/) {
ButtonBig(ButtonText: "Penjurusan",ButtonIcon: "figure.walk")
}
Button(action: /*#START_MENU_TOKEN#*//*#PLACEHOLDER=Action#*/{}/*#END_MENU_TOKEN#*/) {
ButtonBig(ButtonText: "Status",ButtonIcon: "person.crop.circle.badge.plus")
}
}.buttonStyle(PlainButtonStyle()))
RoundedRectangle(cornerRadius: 15)
.frame(width:381, height: 535, alignment: /*#START_MENU_TOKEN#*/.center/*#END_MENU_TOKEN#*/)
.shadow(radius: 4,y:3)
.foregroundColor(.white)
}
I wanted to create something like that basically
if there's a library for it, please do tell me. Thank you
Try these 2 modifiers:
.background(RoundedRectangle(cornerRadius: 10))
.clipped()

How to: Layout in Swift UI using ZStack, VStack, HStack

I am trying to achieve this layout https://gyazo.com/9714f8f1bff98edb3365338563b28fe8 in Swift UI using V and H Stacks. So far I have achieved this https://gyazo.com/fec9ae229e59a41add6542c9a8ad31af. I haven't found the right approach just yet on what to do here for more manipulation. What properties would help me achieve the desired layout?
Update! Here is what I have figured out so far... Almost there.
UPDATED CODE AND CANVAS HERE: https://gyazo.com/d12b9a831f50c25f8f854dc2143c93dc
import SwiftUI
struct CustomBlock: View {
var leading: String
var trailing: String
var body: some View {
VStack {
HStack (alignment: .firstTextBaseline, spacing: 18){
Button(action: {}){
Text(leading)
.bold()
.font(Font.custom("Helvetica Neue", size: 21.0))
.padding(25)
.foregroundColor(Color.white)
.background(Color.green)
.cornerRadius(12)
.multilineTextAlignment(.center)
.fixedSize()
.frame(width: 150, height: 50, alignment: .center)
}
Button(action: {}){
Text(trailing)
.bold()
.font(Font.custom("Helvetica Neue", size: 21.0))
.padding(25)
.foregroundColor(Color.white)
.background(Color.green)
.cornerRadius(12)
.multilineTextAlignment(.center)
.fixedSize()
.frame(width: 150, height: 50, alignment: .center)
}
}
}.padding()
}
}
struct ContentView: View {
var body: some View {
ZStack {
Color.init(hue: 0.2722, saturation: 0.89, brightness: 0.29, opacity: 1.0) .edgesIgnoringSafeArea(.all)
VStack {
HStack {
StrokeText(text: "Dividend Chaser", width: 0.5, color: .black)
.foregroundColor(.white)
.font(.system(size: 45, weight: .bold))
}
ZStack {
RoundedRectangle(cornerRadius: 25, style: .continuous)
.fill( Color.init(red: 0.33, green: 0.56, blue: 0.27))
.frame(width: 350, height: 240)
}
CustomBlock(leading: "Today's List", trailing: "Tomorrow's List").lineLimit(2)
CustomBlock(leading: "This Month", trailing: "Next Month").lineLimit(2)
CustomBlock(leading: "3% Yeild Or Higher", trailing: "5% Yeild Or Higher").lineLimit(2)
CustomBlock(leading: "App Help", trailing: "More Apps").lineLimit(2)
}
}
}
}
Here is a demo of approach. Tested with Xcode 12 / iOS 14.
struct DemoButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.foregroundColor(.white).font(Font.body.bold())
.frame(maxWidth: .infinity).padding(.vertical, 10)
.background(RoundedRectangle(cornerRadius: 8).fill(Color.green))
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity)
}
}
struct TestButtonsGridLayout: View {
var body: some View {
VStack(spacing: 1) {
HStack(spacing: 1) {
Button("Today's\nList") {}
Button("Tomorrow's\nList") {}
}
HStack(spacing: 1) {
Button("This\nMonth") {}
.overlay(RoundedRectangle(cornerRadius: 8)
.stroke(lineWidth: 4).foregroundColor(.white).padding(2))
Button("Next\nMonth") {}
}
// .. other same here
}.buttonStyle(DemoButtonStyle())
}
}
Note: a) button style can be applied per-button b) show overlay selection can be also moved in style and activated by some state variable.

Resources