Swiftui 为什么SF符号的框架不是';不包括它的整个形象?
当我把一个图像放在屏幕上时,我希望图像完全封闭在自己的框架内,但显然我错了。有人知道为什么吗?谢谢 我的代码:Swiftui 为什么SF符号的框架不是';不包括它的整个形象?,swiftui,Swiftui,当我把一个图像放在屏幕上时,我希望图像完全封闭在自己的框架内,但显然我错了。有人知道为什么吗?谢谢 我的代码: struct ContentView: View { var body: some View { HStack { Group { Image(systemName: "envelope.badge.fill") Image(systemName: &qu
struct ContentView: View {
var body: some View {
HStack {
Group {
Image(systemName: "envelope.badge.fill")
Image(systemName: "moon.stars.fill")
} // Group
.foregroundColor(.yellow)
.font(.system(size: 128))
.border(Color.red)
} // VStack
.padding(40)
.background(Color.white)
}
}
结果:
尝试将填充物放在边框之前 编辑。。。 这是否有助于:
HStack {
Group {
Image(systemName: "envelope.badge.fill")
Image(systemName: "moon.stars.fill")
} // Group
.foregroundColor(.yellow)
.font(.system(size: 128))
.frame(width:150, height: 150, alignment: .center)
.padding(10)
.border(Color.red)
} // VStack
.padding(40)
.background(Color.white)
尝试将填充物放在边框之前 编辑。。。 这是否有助于:
HStack {
Group {
Image(systemName: "envelope.badge.fill")
Image(systemName: "moon.stars.fill")
} // Group
.foregroundColor(.yellow)
.font(.system(size: 128))
.frame(width:150, height: 150, alignment: .center)
.padding(10)
.border(Color.red)
} // VStack
.padding(40)
.background(Color.white)
这似乎是Swiftui2.0(Xcode 12)中修复的一个bug 这里是SwiftUI 1.0(Xcode 11)的一个变通方法。它使用
UIImage(systemName:)
来获取图像的实际尺寸,然后使用.aspectRatio(\ucode:contentMode:)
来设置正确的纵横比
struct SystemImage: View {
let name: String
var body: some View {
let uiImage = UIImage(systemName: name)!
return Image(systemName: name)
.resizable()
.aspectRatio(uiImage.size, contentMode: .fit)
}
}
struct ContentView: View {
var body: some View {
HStack {
Group {
SystemImage(name: "envelope.badge.fill")
SystemImage(name: "moon.stars.fill")
} // Group
.foregroundColor(.yellow)
.font(.system(size: 128))
.border(Color.red)
} // VStack
.padding(40)
.background(Color.white)
}
}
这似乎是Swiftui2.0(Xcode 12)中修复的一个bug 这里是SwiftUI 1.0(Xcode 11)的一个变通方法。它使用
UIImage(systemName:)
来获取图像的实际尺寸,然后使用.aspectRatio(\ucode:contentMode:)
来设置正确的纵横比
struct SystemImage: View {
let name: String
var body: some View {
let uiImage = UIImage(systemName: name)!
return Image(systemName: name)
.resizable()
.aspectRatio(uiImage.size, contentMode: .fit)
}
}
struct ContentView: View {
var body: some View {
HStack {
Group {
SystemImage(name: "envelope.badge.fill")
SystemImage(name: "moon.stars.fill")
} // Group
.foregroundColor(.yellow)
.font(.system(size: 128))
.border(Color.red)
} // VStack
.padding(40)
.background(Color.white)
}
}
在Xcode 12 beta版中正常工作。在Xcode 12 beta版中正常工作。