Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: apply background color to rounded rectangle

Tags:

ios

swiftui

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:

enter image description here

What to do here?

Thank you for your help

like image 236
Another Dude Avatar asked Dec 08 '20 15:12

Another Dude


People also ask

What is roundedrectangle in SwiftUI?

RoundedRectangle in SwiftUI | SwiftOnTap A RoundedRectangle is a rectangular Shape with rounded corners that by default, aligns itself inside of the view containing it. It must be created with a specific corner radius or size.

How to draw a border around a button in SwiftUI?

With SwiftUI, you can easily draw a border around a button or text (and it actually works for all views) using the border modifier. Say, for example, you want to create a button like this:

How to build a rectangle in SwiftUI?

With SwiftUI, you can create a rectangle by just calling its method. You should be seeing the pattern here where SwiftUI does all the minor work for you. In this tutorial, you’ll learn what it takes to build a rectangle from scratch in SwiftUI. To follow along with this tutorial, you’ll need some basic knowledge. A basic familiarity with Swift.

How to create a rounded border with rounded corners in CSS?

To create a border with rounded corners, you can draw a rounded rectangle and overlay on the button like this: In the code, we use a RoundedRectangle and its stroke modifier to create the rounded border. You can modify the color and line width to adjust its appearance.


1 Answers

As far as I understood your needs, here is a solution

demo

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)
)
like image 199
Asperi Avatar answered Oct 18 '22 05:10

Asperi