Cannot Set Color of Button's Label Inside Menu in Swiftui

Cannot set color of Button's Label inside Menu in SwiftUI

This is now possible in iOS 15 by setting a Button's role. Documentation

Example:

Menu("Actions") {
Button(role: .destructive, action: { }) {
Label("Whatever", systemImage: "pencil")
}
}

Result:

Result

SwiftUI - Unable to change color of Image icon

I'm not sure what are you trying to to achieve, but probably you just need template rendering mode, like

Image(self.icon)
.renderingMode(.template)
.foregroundColor(.white)

SwiftUI Button Size/Shape/Color Not Changing

You have to use .buttonStyle(PlainButtonStyle()) to get rid of the default button UI.

Button(action: {
print("Delete button tapped!")
}) {
HStack {
Image(systemName: "trash")
.resizable()
.scaledToFit()
.imageScale(.large)
Text("Delete")
}
.padding()
.background(
Capsule().strokeBorder(Color.white, lineWidth: 1.25)
)
}
.accentColor(Color.white)
.buttonStyle(PlainButtonStyle())

How we can change text of button when a button is clicked for list items in SwiftUI?

If I understand the question correctly, try this approach by removing all toggleText and using @Ali Momeni advice:

struct ContentView: View {
@State var showAnswer = false

@State private var selectedRestaurant: Restaurant?

@State private var restaurants = [ Restaurant(name: "Cafe Deadend", image: "cafedeadend"),
Restaurant(name: "Homei", image: "homei"),
Restaurant(name: "Teakha", image: "teakha"),
Restaurant(name: "Cafe Loisl", image: "cafeloisl"),
Restaurant(name: "Petite Oyster", image: "petiteoyster"),
Restaurant(name: "For Kee Restaurant", image: "forkeerestaurant")
]

private func RestaurantRow(restaurant: Restaurant) -> some View {
BasicImageRow(restaurant: restaurant)
.contextMenu {
Button(action: {
showAnswer = true
setFavorite(item: restaurant)
}) {
HStack {
Text(restaurant.isFavorite ? "UnFavorite" : "Favorite")
Image(systemName: "star")
}
}
}.id(restaurant.id)
.onTapGesture {
selectedRestaurant = restaurant
}
.actionSheet(item: $selectedRestaurant) { restaurant in
ActionSheet(title: Text("What do you want to do"),
message: nil,
buttons: [
.default(
Text(restaurant.isFavorite ? "Mark as UnFavorite" : "Mark as Favorite"),
action: {
setFavorite(item: restaurant)
}),
.cancel()
])
}
}

var body: some View {
List {
ForEach(restaurants) { restaurant in
if restaurant.isFavorite {
RestaurantRow(restaurant: restaurant)
} else {
RestaurantRow(restaurant: restaurant)
}
}
}
}

private func setFavorite(item restaurant: Restaurant) {
if let index = restaurants.firstIndex(where: { $0.id == restaurant.id }) {
restaurants[index].isFavorite.toggle()
}
}

}

SwiftUI Button tap only on text portion

This fixes the issue on my end:

var body: some View {
GeometryReader { geometry in
Button(action: {
// Action
}) {
Text("Button Title")
.frame(
minWidth: (geometry.size.width / 2) - 25,
maxWidth: .infinity, minHeight: 44
)
.font(Font.subheadline.weight(.bold))
.background(Color.yellow).opacity(0.8)
.foregroundColor(Color.white)
.cornerRadius(12)

}
.lineLimit(2)
.multilineTextAlignment(.center)
.padding([.leading,.trailing], 5)
}
}

button

Is there a reason why you are using UIScreen instead of GeometryReader?

How to configure ContextMenu buttons for delete and disabled in SwiftUI?

All of the asked situations are now supported in iOS 15

Destructive: (works from iOS 15)

Set .destructive as the role argument of the button:

Button(role: .destructive) { // This argument
// delete something
} label: {
Label("Delete", systemImage: "trash")
}

Delete Demo



Disabled: (works from iOS 14.2)

Add .disabled modifier to the button.

Button {
// call someone
} label: {
Label("Call", systemImage: "phone")
}.disabled(true) // This modifier

Disabled Demo



Divider: (works from iOS 14)

Use a Divider() view directly.

Divider Demo



Full Demo:

Demo
⚠️ Remember! Do not use image instead of systemImage for showing an SFSymbol on the button!



Related Topics



Leave a reply



Submit