簡體   English   中英

SwiftUI ActionSheet 每個動作的顏色不同

[英]SwiftUI ActionSheet different color for each action

我對 SwiftUI 中的操作表有疑問。 我想創建一個帶有 2 個選項的 ActionSheet:刪除和取消。 “刪除”按鈕為紅色,“取消”為綠色。

這是一個代碼示例:

Button(action: {
                    print("Delete button pressed")
                    self.showingActionSheet = true
                }){
                    Text("Go to actions")
                        .foregroundColor(.green)
                        .font(.body)
                        .padding()
                }
                .actionSheet(isPresented: $showingActionSheet) {                   
                    return ActionSheet(title: Text("Delete images"), buttons: [
                        .default(Text("Delete selected").foregroundColor(.red)){
                            // some action to do
                        },
                        .cancel()
                    ])
                }

問題是動作的顏色是兩個按鈕的默認顏色(“藍色”)。 我可以通過在“SceneDelegate.swift”甚至上面的代碼中添加以下行來更改它。

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = UIColor(named: "green")

這條線的問題是它會將一般顏色從“藍色”覆蓋到“綠色”。 仍然需要找到如何為每個動作不同顏色的解決方案。

這是它的樣子:圖像預覽

你有什么建議嗎?

刪除等操作還有另一種按鈕樣式

演示

.actionSheet(isPresented: $showingActionSheet) {
    return ActionSheet(title: Text("Delete images"), buttons: [
        .destructive(Text("Delete selected")){
            // some action to do
        },
        .cancel()
    ])
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM