我試圖讓一系列從互聯網上下載的favicons,然后插入到一個方形的框架中。然而,它們有不同的尺寸,而且用scaledToFit()或scaledToFill()似乎也不能解決這個問題。
下面是代碼:
import SwiftUI
struct ContentView。View {
var websites = ["https://www.reddit.com/favicon.ico"/span>, "https://www.facebook.com/favicon.ico"/span>, "https://www. instagram.com/favicon.ico", "https://www.google.com/favicon.ico"/span>, "https://www.netflix.com/favicon.ico"/span>, "https://www.amazon.com/favicon.ico"]
var body。some View {
NavigationView {
Form {
ForEach(sites, id: .self) { wbs in.
Image(uiImage: try! UIImage(withContentsOfUrl: URL(string: wbs)!)!)
.frame(width: 40, height: 40, alignment: .center)
.scaledToFit()
.clipShape(RoundedRectangle(cornerRadius: 10)
.overlay(RoundedRectangle(cornerRadius: 10)
.strokeBorder(Color.gray, lineWidth: 1.0)
.foregroundColor(.clear))
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
extension UIImage {
convenience init?(withContentsOfUrl url: URL) throws {
let imageData = try Data( contentsOf: url)
self.init(data: imageData)
}
}
我想實作的是,所有的圖示都以同樣的方式填充在框架中。例如,在我附上的截圖中,Reddit和Instagram的圖示看起來不錯,而Facebook太小,Amazon或Netflix太大。
謝謝大家!
uj5u.com熱心網友回復:
添加
.resizable()
對影像進行修改
修改器
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/309074.html
標籤:
上一篇:用Croppie.js和模版裁剪圖片時出現錯位和黑/空白的上傳情況
下一篇:<p>在我的一個學校專案中,我們必須使用Html&CSS來設計一個網站,我遇到了一個問題,我不知道如何將兩個標志(左&右)放在頁腳的角落里。我試著改變位置、浮動、寬度等,但

