Jab*_*or1 6 image swift swiftui geometryreader
因此,我一段时间以来一直在尝试找出如何在 SwiftUI 中查找图像的高度,但没有成功。本质上,我有纵向/横向图像,我想要以大约 600 pt 高显示纵向图像,而我想要显示横向图像,因此使用图像的宽度,因此高度是任意的。话虽这么说,我希望它位于 GeometryReader 内部,因为我使用的偏移量使用它,这样当您向下滚动时,图像不会滚动,但如果您向上滚动,它就会滚动。我不相信我可以共享图像,但无论如何,这应该足以成功运行它!
struct ContentView: View {
@Environment(\.presentationMode) var mode: Binding<PresentationMode>
var backButton: some View {
Button(action: {
withAnimation {
self.mode.wrappedValue.dismiss()
}
}) {
Image(systemName: "chevron.left.circle.fill")
.opacity(0.8)
.font(.system(size: 30))
.foregroundColor(.black)
.background(Color.gray.mask(Image(systemName: "chevron.left").offset(x: 9.4, y: 7.6)))
}
}
var body: some View {
ScrollView(showsIndicators: false) {
GeometryReader { geometry in
Image("image1")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(maxWidth: UIScreen.main.bounds.width, minHeight: 350, maxHeight: 600, alignment: .top)
.clipped()
.overlay(
Rectangle()
.foregroundColor(.clear)
.background(LinearGradient(gradient: Gradient(colors: [.clear, .white]), startPoint: .center, endPoint: .bottom))
)
.offset(y: geometry.frame(in: .global).minY > 0 ? -geometry.frame(in: .global).minY : 0)
}
.frame(minWidth: UIScreen.main.bounds.width, maxWidth: UIScreen.main.bounds.width, minHeight: 350, idealHeight: 600, maxHeight: 600)
//Message
VStack(alignment: .leading) {
Text("\(07/22/20) - Day \(1)")
.font(.body)
.fontWeight(.light)
.foregroundColor(Color.gray)
.padding(.leading)
Text("what I Love)
.font(.title)
.fontWeight(.bold)
.padding([.leading, .bottom])
Text("paragraph")
.padding([.leading, .bottom, .trailing])
}
}
.edgesIgnoringSafeArea(.all)
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: backButton)
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激!
编辑:这里有两张图片供参考 - 抱歉,我没有意识到你能够做到这一点,尽管我应该想到这一点!
如果我理解正确的话,您想要图像的大小,以便可以在自己的框架中使用它的尺寸?您可以创建一个函数,它接受图像名称并返回所需的图像,同时还为您可以在框架中使用的和设置@State变量。widthheight
@State var width: CGFloat = 0
@State var height: CGFloat = 0
func image(_ name: String) -> UIImage {
let image = UIImage(named: name)!
let width = image.size.width
let height = image.size.height
DispatchQueue.main.async {
if width > height {
// Landscape image
// Use screen width if < than image width
self.width = width > UIScreen.main.bounds.width ? UIScreen.main.bounds.width : width
// Scale height
self.height = self.width/width * height
} else {
// Portrait
// Use 600 if image height > 600
self.height = height > 600 ? 600 : height
// Scale width
self.width = self.height/height * width
}
}
return image
}
Run Code Online (Sandbox Code Playgroud)
用法:
GeometryReader { geometry in
Image(uiImage: image("image1"))
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: width, height: height)
.overlay(
Rectangle()
.foregroundColor(.clear)
.background(LinearGradient(gradient: Gradient(colors: [.clear, .white]), startPoint: .center, endPoint: .bottom))
)
.offset(y: geometry.frame(in: .global).minY > 0 ? -geometry.frame(in: .global).minY : 0)
Run Code Online (Sandbox Code Playgroud)
编辑:添加该DispatchQueue.main.async块作为“视图更新期间修改状态”警告的快速修复。可行,但可能不是绝对最好的方法。将图像放入自己的图像中@State并将其设置在视图的onAppear方法中可能会更好。
编辑2:将图像移动到它自己的@Statevar中,我相信这是一个更好的解决方案,因为它将使视图更可重用,因为您可以传递图像而不是在视图中硬编码图像的名称。
@State var image: UIImage {
didSet {
let width = image.size.width
let height = image.size.height
DispatchQueue.main.async {
if width > height {
// Landscape image
self.width = width > UIScreen.main.bounds.width ? UIScreen.main.bounds.width : width
self.height = self.width/width * height
} else {
self.height = height > 600 ? 600 : height
self.width = self.height/height * width
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
GeometryReader { geometry in
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: width, height: height)
.overlay(
Rectangle()
.foregroundColor(.clear)
.background(LinearGradient(gradient: Gradient(colors: [.clear, .white]), startPoint: .center, endPoint: .bottom))
)
.offset(y: geometry.frame(in: .global).minY > 0 ? -geometry.frame(in: .global).minY : 0)
Run Code Online (Sandbox Code Playgroud)
必须通过传入 UIImage 来更新 ContentView 的声明方式:
ContentView(image: UIImage(named: "image0")!)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11082 次 |
| 最近记录: |