gpt4 book ai didi

image - SwiftUI 将图像固定到屏幕右上角并根据设备大小调整大小

转载 作者:行者123 更新时间:2023-12-05 00:57:59 25 4
gpt4 key购买 nike

我刚刚学习了 Apple 的 SwiftUI,即使做基础知识也有点令人沮丧。我正在尝试将我的图像放在屏幕的右上角,但默认情况下它显示在我的屏幕中央。任何人都知道我怎么能做到这一点。此外,我尝试让图像根据屏幕尺寸调整大小,但我以前在常规 Swift 中使用的旧 self.frame.width 在 Swift UI 中不起作用。抱歉,但是这种在 SwiftUI 中编写代码的新方式对我来说很奇怪。

var body: some View {
ZStack {
//Define a screen color
LinearGradient (gradient: Gradient(colors:[Color(ColorsSaved.gitLabDark),Color(ColorsSaved.gitLabLight)]),startPoint: .leading,endPoint: .trailing)

//Extend the screen to all edges
.edgesIgnoringSafeArea(.all)

VStack(alignment: .trailing) {
Image("blobVectorDark")
.resizable()

.edgesIgnoringSafeArea(.top)

.frame(width: 60, height: 60)
// .offset(x: , y: 20)

}
}

}
}

最佳答案

你需要为外部容器添加一个框架,这里是 VStack。

然后为这个容器分配对齐方式。

VStack框架内的宽高需要使用geometryProxy。

GeometryReader{ (proxy : GeometryProxy) in  // New Code
VStack(alignment: .trailing) {
Image("blobVectorDark")
.resizable()

.edgesIgnoringSafeArea(.top)

.frame(width: 60, height: 60)
// .offset(x: , y: 20)

}
.frame(width: proxy.size.width, height:proxy.size.height , alignment: .topLeading) // New Code
}

关于image - SwiftUI 将图像固定到屏幕右上角并根据设备大小调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59042144/

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