我对SwiftUI还是陌生的,我正尝试遵循WWDC-2019应用程序演示文稿,该演示文稿名为“ SwiftUI简介:构建您的第一个应用程序”。到目前为止,一切正常,除了图像未按预期呈现在视图上。我的资产xcasette已正确加载了九个缩略图图像,并且每个图像都贴有房间名称。以下是我的contentView代码:
import SwiftUI
struct ContentView: View {
var rooms: [Room] = []
var body: some View {
List(rooms) { room in
Image(room.thumbnailName)
VStack (alignment: .leading){
Text(room.name)
Text("\(room.capacity) people")
.font(.subheadline)
.foregroundColor(.secondary)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(rooms: testData)
}
}
下面的代码是一个可识别的结构,称为“ Room”,数据名为“ testData”
import SwiftUI
struct Room : Identifiable {
var id = UUID()
var name: String
var capacity: Int
var hasVideo: Bool = false
var imageName: String { return name}
var thumbnailName: String { return name + "Thumb"}
}
let testData = [
Room(name: "Observation Deck", capacity: 6, hasVideo: true),
Room(name: "Executive Suite", capacity: 8, hasVideo: false),
Room(name: "Charter Jet", capacity: 16, hasVideo: true),
Room(name: "Dungeon", capacity: 10, hasVideo: true),
Room(name: "Panorama", capacity: 12, hasVideo: false),
Room(name: "Oceanfront", capacity: 8, hasVideo:false),
Room(name: "Rainbow Room", capacity: 10, hasVideo: true),
Room(name: "Pastoral", capacity: 7, hasVideo: false),
Room(name: "Elephant Room", capacity: 1, hasVideo: false)
]
我尝试将缩略图的名称更改为“ thumbnailName”,“ Thumb”,“ room”,但均无效。我无法将“ Thumb”一词与代码中的任何内容相关联,我怀疑这可能是问题的根源。
根据要求,以下是资产内容的屏幕截图:
答案 0 :(得分:0)
换行 var thumbnailName:字符串{返回名称+“缩略图”}
收件人
var thumbnailName:字符串{返回名称}
仅在某处添加“拇指”,图像不会自动更改其大小。您可以在资产中添加带有“ thumb”名称的小图像,也可以通过添加可调整大小和框架来更改图像的大小。
答案 1 :(得分:0)
仅通过在文件ROOM中添加“ thumb”,图像不会自动更改其大小,在该文件中,您的结构代码然后在运行后退出单词“ thumb”,并将下一行代码放到Image(room.thumbnailName )
ROMS文件:
var thumbnailName: String {return name}
contentView:
Image (room.thumbnailName)
.resizable()
.frame(width: 32.0, height: 32.0)