我可以在 Swift UI 中更改图像大小吗

Por*_*Por 1 swift swiftui

是在 swift ui 中设置图像宽度和高度大小的任何方法。我找到了框架方法,但这不是我想要的。

这是我的快速文件。

 import SwiftUI

struct HeaderBar: View {

    var body: some View {

        VStack(alignment: .center, spacing: 0.0) {

            HStack(alignment: .center) {
                Spacer()
                Image("jojologo-yellow")
                .frame(width: 30.0, height: 30.0) //that is not the solution to change image size
                .padding()
                Spacer()
            }
            .background(Color.orange)
            Spacer()

        }

    }
}

struct HeaderBar_Previews: PreviewProvider {
    static var previews: some View {
        HeaderBar()
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我可以在 Image 中使用 resize 方法,那就太好了。例如

Image("jojologo-yellow").size(width: 30.0, height: 30.0)
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是“有什么方法可以在 swift ui 中调整图像大小

Joe*_*Joe 7

也许Image("jojologo-yellow").resizable().frame(width: 30.0, height: 30.0)这就是你要找的?


Roh*_*ana 6

将修饰符添加.resizable()到您的代码中

  Image("yourImageName")
    .resizable()
    .scaledToFill() // add if you need
    .frame(width: 30.0, height: 30.0) // as per your requirement
    .clipped()
Run Code Online (Sandbox Code Playgroud)