简体   繁体   English

SwiftUI VStack 对齐右单个元素

[英]SwiftUI VStack align right single element

I'm new at SwiftUI, I'm trying this time to align right a single element (an image) to the right and then the rest of the content should be center aligned.我是 SwiftUI 的新手,这次我尝试将单个元素(图像)右对齐,然后内容的 rest 应该居中对齐。

Just like what happens when you use Spacer() on an HStack but to the other side.就像您在HStack上使用Spacer()但在另一侧使用时发生的情况一样。

I read about .alignmentGuide and it was a little bit confusing but I tried using it this way:我读到了.alignmentGuide ,这有点令人困惑,但我尝试以这种方式使用它:

struct ContentView: View {
    var body: some View {
        VStack {
            HStack {
                Text("Hello!").alignmentGuide(.trailing) {
                    v in v[HorizontalAlignment.trailing]
                }
            }
            Text("Hello, World!")
            Spacer()
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

But this makes no change at all, both Text are center aligned.但这根本没有改变,两个Text都是中心对齐的。

I tried moving the .alignmentGuide code after the } of the HStack with the same result.我尝试在HStack}之后移动.alignmentGuide代码,结果相同。

This is what I'm trying to do, before with storyboards I'd use some constraints to do this, like setting the right constraint to 8 or something, but I'm a bit lost when it comes to doing it the SwiftUI way.这就是我正在尝试做的事情,在使用故事板之前,我会使用一些约束来做到这一点,比如将正确的约束设置为 8 或其他东西,但在使用 SwiftUI 方式时我有点迷茫。

在此处输入图像描述

Here's one way to do it:这是一种方法:

struct ContentView: View {
    var body: some View {
        VStack {
            HStack {
                Spacer()
                Text("Hello!")
                    .padding(.trailing, 8)
            }
            Text("Hello, World!")
            Spacer()
        }
    }
}

Use an HStack with a Spacer() to push Text("Hello!") to the right.使用带有Spacer()HStackText("Hello!")推到右侧。 Add .padding(.trailing, 8) to keep it 8 points off the right edge.添加.padding(.trailing, 8)以使其离开右边缘 8 点。

To answer to @cheesus, I would say @vacawama's answer is not the recommended way of doing it.要回答@cheesus,我会说@vacawama 的回答不是推荐的做法。

Here is how I would do it这是我会怎么做

VStack {
  Text("Hello!")
    .frame(maxWidth: .infinity, alignment: .trailing)
    .padding(.trailing, 8)
  Text("Hello, World!")
}
.frame(maxHeight: .infinity, alignment: .top)

To elaborate a bit more再详细一点

The main problem with using (H|V)Stack s and Spacer s is that you get a fixed spacing between your content and the Spacer , and in some cases it might cause issues.使用(H|V)StackSpacer的主要问题是你的内容和Spacer之间的间距是固定的,在某些情况下它可能会导致问题。

An example一个例子

  • Consider we want a HStack , some spacing between elements and the last text aligned to the left:考虑我们想要一个HStack ,元素之间有一些间距,最后一个文本左对齐:

     HStack(spacing: 25) { Color.red.frame(width: 50, height: 20) Color.green.frame(width: 50, height: 20) Text("The text").background(Color.blue) Spacer() }.frame(width: 250).border(Color.yellow).padding(8)

    It gives:它给: 示例 1

  • If the text becomes longer, you see the HStack spacing between the spacer:如果文本变长,您会看到分隔符之间的HStack间距:

     - Text("The text") + Text("Some long text")

    示例 2

  • Now I just replaced the Spacer with a Color so you can see it in action:现在我只是用Color替换了Spacer ,这样你就可以看到它的实际效果:

     - Text("Some long text") -.background(Color.blue) - Spacer() + Text("Some long text") +.background(Color.blue) +.layoutPriority(1) + Color.indigo +.frame(height: 20) +.frame(maxWidth: .infinity)

    示例 3

  • This is fixed if you use .frame(alignment:) :如果您使用.frame(alignment:) ,这是固定的:

     HStack(spacing: 25) { Color.red.frame(width: 50, height: 20) Color.green.frame(width: 50, height: 20) Text("Some long text").frame(maxWidth: .infinity, alignment: .leading).background(Color.blue) }.frame(width: 250).border(Color.yellow).padding(8)

    示例 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM