[英]SwiftUI: How to set custom view size dynamically
也許是以下的一些變體:
struct CustomView: View {
var body: some View {
HStack(spacing: 10) {
Image(systemName: "heart.fill")
.resizable()
.aspectRatio(1, contentMode: .fit)
Image(systemName: "heart.fill")
.resizable()
.aspectRatio(1, contentMode: .fit)
}.frame(maxWidth: .infinity)
}
}
兩個圖像保持 1:1 的縱橫比,它們之間有 10 個點的間距,但允許使用.frame
修飾符水平增長。
根據需要將Image(systemName: "heart.fill")
替換為所需的圖像源。
如果您的源圖像資產沒有 1:1 的縱橫比,請將contentMode
設置為.fill
,並使用.frame(height: ..)
限制HStack
或CustomView
內的CustomView
.frame(height: ..)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.