我一直试图制作一个按钮列表,当单击它时,它会调用一些代码,我还需要一些“缩略图”来配合每个按钮,因此我为每个按钮制作了一个HStack并放入全部显示在列表上,现在不显示缩略图,顺便说一句,它可以很好地呈现系统图像和本地资源,只是从网络上获取的图像
您只需将其复制粘贴到ContentView.swift中即可复制
import SwiftUI
func artwork() -> Image
{
let art = "https://via.placeholder.com/150"
if let url = URL(string: art), let data = try? Data(contentsOf: url), let image = UIImage(data: data)
{
return Image(uiImage: image)
}
else
{
return Image(systemName: "book")
}
}
struct ContentView: View
{
var body: some View
{
List
{
Button(action: {...my code...})
{
HStack
{
artwork()
}
... some other info
}
}
}
}
答案 0 :(得分:2)
此问题类似于Avoid button styling its content in SwiftUI
您只需像这样将.renderingMode(.original)
添加到Image
:
func artwork() -> Image
{
let art = "https://via.placeholder.com/150"
if let url = URL(string: art), let data = try? Data(contentsOf: url), let image = UIImage(data: data)
{
return Image(uiImage: image)
}
else
{
return Image(systemName: "book")
}
}
struct ContentView: View
{
var body: some View
{
List
{
Button(action: {})
{
HStack
{
artwork().renderingMode(.original)
}
}
}
}
}