SwiftUI无法调整图像大小

时间:2019-11-07 07:07:55

标签: ios swift swiftui

我正在尝试在SwiftUI中创建图像,并且面临的问题是,即使设置了内容模式填充,图像也无法调整自身大小

struct LoginView: View {
    var body: some View {
        Image("app-icon")
                .frame(width: percent(80, ofSize: "width"), height: percent(40, ofSize: "width"), alignment: Alignment.center)
                .aspectRatio(contentMode: ContentMode.fill)
                .scaledToFill()
                .clipped()

    }
}

然后我尝试了

struct LoginView: View {
    var body: some View {
        Image("app-icon")
                .frame(width: percent(80, ofSize: "width"), height: percent(40, ofSize: "width"), alignment: Alignment.center)
                .aspectRatio(contentMode: ContentMode.fit)
                .scaledToFit()
                .clipped()

    }
}

enter image description here

两个代码都具有相同的效果,图像大小不变

2 个答案:

答案 0 :(得分:2)

  • 在代码中添加.resizable()修饰符

    Image("app-icon")
       .resizable()
       .scaledToFill()
       .frame(width: percent(80, ofSize: "width"), height: percent(40, ofSize: "width"), alignment: Alignment.center)
       .clipped()
    

答案 1 :(得分:1)

必须添加.resizable()作为Image之后的第一个修改器,其他修改器才能生效。