![](/img/trans.png)
[英]How to change "Cancel" button text in modal sheet view in SwiftUI on watchOS 7?
[英]SwiftUI - Action sheet with no title, Alert.Button using View instead of Text
有什么辦法可以讓操作表中沒有標題部分嗎? SwiftUI代碼中的Action Sheet定義如下:
public struct ActionSheet {
/// Creates an action sheet with the provided buttons.
public init(title: Text, message: Text? = nil, buttons: [ActionSheet.Button] = [.cancel()])
/// A button representing an operation of an action sheet presentation.
public typealias Button = Alert.Button
}
由於 title 只符合 Text,我想也許只添加 Text("") 就可以了; 但是,這反而使標題的空間為空,而不是將其刪除。 給 nil 而不是 Text("") 也不起作用。
另外,有什么方法可以給操作表的按鈕一個視圖,如下所示:
struct ActionItem: View {
var body: some View {
HStack {
Image(systemName: "phone")
Spacer()
Text("Call 1-408-123-4567")
}
}
}
最簡潔的答案是不。 SwiftUI 當前的實現將始終為標題騰出空間,並且只會為其按鈕使用Text
視圖。
目前尚不清楚這是否是因為 Apple 在今年發布 SwiftUI 之前只有這么多時間,並且想要解決最簡單和最常見的用例,或者他們是否采取了 ActionSheets 應該始終具有標准外觀的原則立場,包括標題和只有文本按鈕。 我們將不得不拭目以待。
Apple 已通過 iOS 15 和新推出的confirmationDialog
API 響應了我們的電話ActionSheet
也已在此版本中棄用。
ConfirmationDialog
已添加為視圖修飾符,可用於任何視圖,與我們之前使用的.actionSheet
API 非常相似。 使用新對話框時,我們可以指定隱藏標題,並可以使用 Button API 作為role
來控制按鈕外觀。
下面是在簡單視圖上使用它的示例。
struct ContentView: View {
@State private var isShowingDialog: Bool = false
var body: some View {
VStack {
Button {
isShowingDialog = true
} label: {
Text("Tap Me")
}
.confirmationDialog("", isPresented: $isShowingDialog, titleVisibility: .hidden) {
Button("Normal") {
}
Button("Delete", role: .destructive) {
}
Button("Cancel", role: .cancel) {
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.