【问题标题】:SwiftUi: How to reduce space between frame and Text?SwiftUi:如何减少框架和文本之间的空间?
【发布时间】:2020-01-15 18:30:35
【问题描述】:

我尝试在 SwiftUI 中创建一个 iOS 视图,其中矩形作为 Stacks 周围的框架,但我无法减少 TextHStack 框架之间的空间。

所以详细地说:我使用了一个VStack,里面有多个HStacks。当我尝试减少 2 HStacks 之间的空间时会出现问题。

在下面的代码 sn-p 中,第一个 Text“标题”在 HStack 中,Spacer 后跟另一个 HStack,其中有 2 个 Text 元素。

我想让第一个Text“标题”直接位于矩形上方。没有像我现在这样的大空间。

有什么建议吗? 非常感谢你。

代码:

var body: some View {   

     VStack() {
         HStack() {
             Text("Title")
                 .font(Font.body)
                 .cornerRadius(ViewConstants.CORNERRADIUS)
                 .padding()
                 .foregroundColor(Colours.BLUE)
             Spacer()
         } 
         HStack(alignment: .firstTextBaseline) {
             Spacer()
             Text("SomeTextText")
                 .font(Font.bold(.body)())
                 .cornerRadius(ViewConstants.CORNERRADIUS)
                 .padding()
                 .foregroundColor(Color.gray)
                 .clipped()
             Text("xx,00")
                 .font(Font.body)
                 .cornerRadius(ViewConstants.CORNERRADIUS)
                 .padding()
                 .foregroundColor(Color.gray)
             Spacer()
         } // HStack
             .overlay(
                 RoundedRectangle(cornerRadius: ViewConstants.CORNERRADIUS)
                     .stroke(Colours.BLUE, lineWidth: 1)
                 )   .padding(.init(top: 0, leading: 3, bottom: 0, trailing: 3))
...

【问题讨论】:

  • 你能附上截图吗?

标签: ios stack swiftui frame


【解决方案1】:

我想让第一个文本“标题”直接位于矩形上方。不 像我现在这样的大空间。

我认为这是预期的

Text("Title")
    .font(Font.body)
    .cornerRadius(ViewConstants.CORNERRADIUS)
    .padding(.horizontal) // << only horizontal padding

【讨论】:

  • 谢谢。好像我双眼都瞎了:D。非常感谢。
猜你喜欢
  • 2013-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多