首页 > 解决方案 > SwiftUI 按钮图像插图

问题描述

SwiftUI 是否有与 ImageEdgeInsets 相同的东西。例如,我有 60x60 的按钮。并想为里面的图像制作插图。

VStack(alignment: .leading, spacing: 0.0){
            Section{
                Button(action: {
                    if self.accountViewModel.signInContext == .quote{
                        self.presentationMode.wrappedValue.dismiss()
                    }else{
                        self.viewController?.dismiss(animated: true)
                    }
                }){
                    Image("Close Icon").renderingMode(.template).padding(.vertical, 20).padding(.horizontal, 20).foregroundColor(.white).scaledToFit()
                }
                .frame(width: 60, height: 60)
                Rectangle().line().padding(.horizontal, -30)
            }
        }

标签: swiftui

解决方案


是的,.padding就是为了这个目的,只需要使图像可调整大小。我想你需要以下

Image("Close Icon")
    .renderingMode(.template)
    .resizable()
    .aspectRatio(contentMode: .fit)
    .padding(.vertical, 20).padding(.horizontal, 20)

推荐阅读