我在使用SwiftUI创建一些视图时,遇到了一个与VStack的框架尺寸有关的奇怪错误。渐变并不能填满屏幕的所有高度。
是Xcode错误还是我做错了什么?
struct WelcomeView : View {
private let colors = [
Color(red: 29/255, green: 151/255, blue: 108/255),
Color(red: 147/255, green: 249/255, blue: 185/255)
]
let screenSize = UIScreen.main.bounds
var body: some View {
VStack {
Text("Content here")
}
.frame(width: screenSize.width, height: screenSize.height)
.background(LinearGradient(gradient: Gradient(colors: colors),
startPoint: .bottom, endPoint: .top), cornerRadius: 0)
}
}
最佳答案
var body: some View {
VStack {
Spacer()
Text("Content here")
Spacer()
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(LinearGradient(gradient: Gradient(colors: colors), startPoint: .bottom, endPoint: .top), cornerRadius: 0)
.edgesIgnoringSafeArea(.all)
}
来源:https://www.hackingwithswift.com/quick-start/swiftui/how-to-place-content-outside-the-safe-area
关于ios - VStack的框架大小无法覆盖整个屏幕,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57480619/