是否可以在SwiftUI`.alert`中显示图像?

时间:2020-10-16 12:35:26

标签: swiftui

Alert(title: <#T##Text#>, message: <#T##Text?#>, ...)的标题和消息仅作为Text而不是View。但是Text可以显示图像。那为什么不能将图像显示为标题或消息呢?

这是一个带有警报的小操场,试图显示图像作为其标题,但仅显示一些空白区域。尽管没有错误出现。

import SwiftUI
import PlaygroundSupport

struct ContentView: View {
    @State private var showAlert = false
    var body: some View {
        
        VStack {
            Text(Image(systemName: "info.circle")) // Just to ensure `Text` can display `Image`
            Button("Show Alert") {
                self.showAlert = true
            }
            .alert(isPresented: $showAlert) {
                Alert(title: Text(Image(systemName: "info.circle")),
                      message: Text("Alert message").bold() /* bold is not working */,
                      dismissButton: .default(Text("OK")))
                
            }
        }
        .frame(width: 300, height: 600) // Sorry about it
    }
}

PlaygroundPage.current.setLiveView(ContentView().previewDevice(PreviewDevice(stringLiteral: "iPhone 7")))
//`.previewdevice` has no effect in playground

0 个答案:

没有答案