Display curved button with custom color - swiftui

I am trying to achieve a Scrollview with buttons that have rounded corners and a custom color.
Button(shoppingListItem.text) {
removeFromShoppingList(itemId: shoppingListItem.item_id)
}
.overlay(
RoundedRectangle(cornerRadius: 20)
.stroke(Color.secondary, lineWidth: 2)
.background(
RoundedRectangle(cornerRadius: 20, style: .continuous)
.fill(Color("ShoppingListItemColor"))
)
)
)
This results in a button with rounded corners and the wanted color but no text is visible. What am I missing here?

You might be better writing your code this way...
Button(shoppingListItem.text) {
removeFromShoppingList(itemId: shoppingListItem.item_id)
}
.overlay(
RoundedRectangle(cornerRadius: 20)
.stroke(Color.secondary, lineWidth: 2)
)
.background(Color("ShoppingListItemColor"))
.clipShape(RoundedRectangle(cornerRadius: 20))
In this I have moved the background out of the overlay.
So this is now built from a button with text.
The button has an overlay of just the border of a rounded rectangle.
Then the button has a background color.
And then the button is clipped by a rounded rectangle.
In your code (probably because of the lack of formatting) you had inadvertently added the background to the overlay. So you had overlaid the whole button with the background colour and covered the text.

Related

How to display same background color when sheet is opened?

I have a page which has a dark blue background color. When a button is pressed, a sheet is opened, however behind the sheet the blue background shifts down and it shows a white background. How can I prevent this?
ZStack {
setBackgroundColor.darkBlue
.ignoresSafeArea(.all)
HStack {
Button(action: {
self.showSheet = true
}) {
Text("Add exercise")
.frame(maxWidth: .infinity)
.frame(height: 10)
.foregroundColor(CustomColor.darkBlue)
.padding()
.background(CustomColor.cyan)
}
.clipShape(Capsule())
.sheet(isPresented: $showSheet) {
AddWorkoutSheet()
}
}
.padding(.bottom)
}
Blue background shifted when sheet is opened.
I have tried to find out what is wrong, but I cannot seem to figure it out. I quite new to programming and SwiftUI.
It is not shifting down, it is just how sheet works. When you toggle a sheet, it will pop out from the bottom with a default white background, therefore covering the background of your root view. So what you want might be a transparent sheet. If this is the case, using sheet may not be the best way as implementing a transparent sheet needs some walkaround. You may instead try using a transition to move the view up or down with offset.

SwiftUI TextEditor expands incorrectly in a VStack with maxWidth

I have two TextEditors in a VStack in my app. I would like each of them to expand to fit the text they contain, but remain hugging the text. Unfortunately, long text in the first TextEditor expands both TextEditors equally. It is as if the total height of the VStack they are in is expanded correctly, but the extra height is shared between the two editors.
This only occurs when I use a maxWidth on the VStack. If I fix the width it behaves correctly.
Is there some way to solve this without losing the resize behaviour maxWidth gives?
ScrollView {
VStack(alignment: .leading) {
TextEditor(text: Binding($note.details)!)
.font(.body)
TextEditor(text: Binding($note.title)!)
.font(.title)
}.frame(maxWidth: 450)
}
Try to put .fixedSize(horizontal: false, vertical: true) on both editors.

How to align Text to a Label's text instead of it's image in SwiftUI?

If I have a Label that uses a system image and some text, let's call the text "title". How would I align the subtitle to the the leading edge of the title text and not the image?
This seems to work, but is there another way? Something just seems off about setting the top alignment.
HStack(alignment: .top) {
Image(systemName: "gift")
VStack(alignment: .leading) {
Text("Title")
Text("Subtitle")
}
}

SwiftUI change sheet background

How can I change a modals sheet background? For instance, if I have:
Button(action: showAdditionalOptions) {
Image(systemName: "ellipsis")
.font(.system(size: 20))
.foregroundColor(Color.white)
}
.onTapGesture { showAdditionalOptions() }
.fullScreenCover(isPresented: $isShowingAdditionalOptions) {
AdditionalActions()
.background(Color.black.opacity(0.2))
}
The effect I am trying to get to is a blur effect that when a sheet is presented, its background is blurred and partly transparent. However I can't change the sheet's background. I can only modify a sheet's content background. Is there a way to apply a background to the sheet itself?

How to make a Textfield with multiple lines and cursor start at top-left?

I'm studying SwiftUI and when I using TextField then I have a problem with TextField's cursor is still at left-center of TextField's area.
How can I set it to left-top of TextField area.
Thanks!
This is my code:
TextField("What is your mind?", text: $statusContent)
.clipped()
.frame(height: 100, alignment: .leading)
.multilineTextAlignment(.leading)