以下内容应该创建一个Text边界,该边界占据整个屏幕,但似乎什么也没做。
struct ContentView: View {
var body: some View {
Text("foo")
.relativeSize(width: 1.0, height: 1.0)
.background(Color.red)
}
}
Run Code Online (Sandbox Code Playgroud)
以下黑客:
extension View {
/// Causes the view to fill into its superview.
public func _fill(alignment: Alignment = .center) -> some View {
GeometryReader { geometry in
return self.frame(
width: geometry.size.width,
height: geometry.size.height,
alignment: alignment
)
}
}
}
struct ContentView2: View {
var body: some View {
Text("foo")
._fill()
.background(Color.red)
}
}
Run Code Online (Sandbox Code Playgroud)
似乎工作。
这是带有的SwiftUI错误relativeSize,还是我缺少某些东西?
You need to watch WWDC 2019 Session 237: Building Custom Views with SwiftUI, because Dave Abrahams discusses this topic, and uses Text in his examples.
To restate briefly what Dave explains in detail:
Thus you cannot force a small Text to fill the screen, because the Text will refuse to consume more space than needed to fit its content.
If you want the red background to fill the screen, you can wrap the Text in an expandable view, force the expandable view to full size, and set the background of the expandable view. This works:
var body: some View {
ZStack {
Spacer()
Text("Hello")
}
.background(Color.red)
}
Run Code Online (Sandbox Code Playgroud)
The Spacer accepts whatever size its parent offers, so the ZStack can't shrink down to the size of just the Text.
| 归档时间: |
|
| 查看次数: |
780 次 |
| 最近记录: |