gpt4 book ai didi

ios - 为什么当附加到 nil 图像时,帧修饰符仍然会影响其他 View ?

转载 作者:行者123 更新时间:2023-12-01 19:29:52 24 4
gpt4 key购买 nike

self.imagesViewModel.allImages[post.imageContentURL]?
.resizable()
.scaledToFill()
.frame(width: 343, height: 171, alignment: .center)
.clipShape(Rectangle())
.shadow(radius: 1)
当返回的图像值为 nil 时,这段代码应该什么都不做。 .但无论出于何种原因, .frame修饰符仍然影响 ScrollView 内的其他元素。它最终会在物理上阻碍导航链接,但在视觉上不会。
我已经确认当 .frame被删除,一切都按我的意愿工作。但是我需要对图像设置限制,所以我不能没有这种东西。
我尝试将代码放在 if 语句中以检查 nil并且仅在返回图像时显示,但这不起作用。此代码的其他变体也不起作用。
if self.imagesViewModel.allImages[post.imageContentURL] != nil {
self.imagesViewModel.allImages[post.imageContentURL]?
.resizable()
.scaledToFill()
.frame(width: 343, height: 171)
.clipShape(Rectangle())
.shadow(radius: 1)
}
我也尝试将最小值设置为零,但没有运气。
我想知道这是否与我使用 Xcode 12 beta 5 或使用新的 SwiftUI 应用程序生命周期有关。当我在我的 iPhone (iOS 14) 上运行代码时,我得到了相同的物理障碍,但在 ScrollView 元素之间添加了额外的视觉间距。
这是我的代码的简化版本,它复制了我的问题:
import SwiftUI
import Combine

struct Post: Codable, Identifiable {
var id: Int
var text: String
var imageContentURL: String?
}

class PostsViewModel: ObservableObject {
@Published var posts: [Post] = []
}

class ImagesViewModel: ObservableObject {
@Published var allImages: [String?: Image] = [:]
}

/// The View of the content displayed in Home
struct PostLayout: View {
@EnvironmentObject var imagesViewModel: ImagesViewModel
var post: Post

init(post: Post) {
self.post = post
}

var body: some View {
VStack {
// This is the navigation link that gets obstructed
NavigationLink(destination: SomeOtherView()) {
Image(systemName: "circle.fill")
.resizable()
.frame(width: 48, height: 48)
.clipShape(Circle())
}

Text(self.post.text)

// MARK: This is the problem code, border added for clarity
self.imagesViewModel.allImages[self.post.imageContentURL]?
.resizable()
.scaledToFill()
.frame(width: 343, height: 171)
.clipShape(Rectangle())
.shadow(radius: 1)
.border(Color.black, width: 1)
}
.border(Color.black, width: 1)
}
}

/// View that is navigated to from Home
struct SomeOtherView: View {
var body: some View {
Text("SomeOtherView")
}
}

/// Main displayed view
struct Home: View {
@EnvironmentObject var postsViewModel: PostsViewModel
@EnvironmentObject var imagesViewModel: ImagesViewModel

var body: some View {
NavigationView {
ScrollView {
ForEach(self.postsViewModel.posts) { post in
PostLayout(post: post)
}
}
.onAppear() {
// All displayed content created here
var post1 = Post(id: 0, text: "Content")
let post2 = Post(id: 1, text: "Content")
var post3 = Post(id: 2, text: "Content")
let post4 = Post(id: 3, text: "Content")
let imageURL = "someImageURL"

self.imagesViewModel.allImages[imageURL] = Image(systemName: "circle")

// Only two posts are supposed to have an imageURL
post1.imageContentURL = imageURL
post3.imageContentURL = imageURL
self.postsViewModel.posts.append(contentsOf: [post1, post2, post3, post4])
}
.navigationTitle("Home")
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Home()
.environmentObject(PostsViewModel())
.environmentObject(ImagesViewModel())
}
}

最佳答案

正如您已经注意到 .frame修饰符不是特定于图像的。即使图像是 nil,它也可以工作.
您可以做的是检查 Image可以加载。您可以使用 UIImage(named:) 来做到这一点。 (返回一个可选的):

@ViewBuilder
var body: some View {
if UIImage(named: "imageName") != nil {
Image("imageName")
.resizable()
.scaledToFill()
.frame(width: 343, height: 171, alignment: .center)
...
}
}
或者,或者:
@ViewBuilder
var body: some View {
if imagesViewModel.allImages[post.imageContentURL] != nil {
imagesViewModel.allImages[post.imageContentURL]!
.resizable()
.scaledToFill()
.frame(width: 343, height: 171, alignment: .center)
...
}
}

编辑
不需要可选的 String?在字典中。只需将其替换为:
@Published var allImages: [String: Image] = [:]
然后使用 if检查图像是否不为零的语句(在 Xcode 12 中,您也可以使用 if-let):
if self.post.imageContentURL != nil {
self.imagesViewModel.allImages[self.post.imageContentURL!]?
.resizable()
.scaledToFill()
.frame(width: 343, height: 171)
.clipShape(Rectangle())
.shadow(radius: 1)
.border(Color.black, width: 1)
}

关于ios - 为什么当附加到 nil 图像时,帧修饰符仍然会影响其他 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63552394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com