xcode 在 SwiftUI 中水平居中视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56462647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:57:17  来源:igfitidea点击:

Center View horizontally in SwiftUI

iosswiftxcodeuser-interfaceswiftui

提问by SwiftiSwift

How can I center horizontally a View(Image) in an HStack? I want a button to be left aligned and the image to be centered horizontally the view.

如何在a 中水平居中 a View( Image) HStack?我想要一个按钮左对齐,图像水平居中视图。

Currently I have this structure:

目前我有这个结构:

VStack {
        HStack {
            Button(action: {
                print("Tapped")
            }, label: {
                Image("left-arrow")
                    .resizable()
                    .frame(width: 30, height: 30, alignment: .leading)
            }).padding(.leading, 20)

            Spacer()

            Image("twitter-logo")
                .resizable()
                .frame(width: 30, height: 30, alignment: .center)

        }
        Spacer()
    }

Which is giving me this:

这给了我这个:

enter image description here

在此处输入图片说明

But I want to achieve this:

但我想实现这一目标:

enter image description here

在此处输入图片说明

Update:

更新:

enter image description here

在此处输入图片说明

采纳答案by Artem Novichkov

What's about saving button size to a property and add a negative padding to the image? And pay attention to an additional spacer after the image.

将按钮大小保存到属性并为图像添加负填充是什么意思?并注意图像后的额外间隔。

struct ContentView: View {

    var buttonSize: Length = 30

    var body: some View {
        VStack {
            HStack {
                Button(action: {
                    print("Tapped")
                }, label: {
                    Image(systemName: "star")
                        .resizable()
                        .frame(width: buttonSize, height: buttonSize, alignment: .leading)
                })
                Spacer()
                Image(systemName: "star")
                    .resizable()
                    .frame(width: 30, height: 30, alignment: .center)
                    .padding(.leading, -buttonSize)
                Spacer()
            }
            Spacer()
        }
    }
}

The result:

结果:

enter image description here

在此处输入图片说明

回答by RPatel99

You can embed two HStack's in a ZStackand place spacers accordingly for the horizontal spacing. Embed all that in a VStackwith a Spacer()to have everything pushed up to the top.

您可以HStack在 a 中嵌入两个,ZStack并相应地为水平间距放置垫片。将所有内容嵌入VStackaSpacer()以将所有内容推到顶部。

struct ContentView : View {
var buttonSize: Length = 30
var body: some View {
    VStack {
        ZStack {
            HStack {
                Button(action: {

                }, label: {
                    Image(systemName: "star")
                        .resizable()
                        .frame(width: CGFloat(30), height: CGFloat(30), alignment: .leading)
                }).padding(.leading, CGFloat(20))

                Spacer()
            }

            HStack {
                Image(systemName: "star")
                    .resizable()
                    .frame(width: CGFloat(30), height: CGFloat(30), alignment: .center)
            }
        }
        Spacer()
    }
}

}

}

Note: In the second HStack, the image should automatically be center aligned, but if it isn't, you can place a Spacer()before and after the image.

注意:在第二个中HStack,图像应该自动居中对齐,但如果不是,您可以Spacer()在图像前后放置一个。

Edit: Added the VStackand Spacer()to move everything to the top like the OP wanted.

编辑:添加VStackSpacer()将所有内容移动到顶部,就像 OP 想要的那样。

Edit 2: Removed padding on image because it caused the image to be slightly offset from the center. Since it is in its own HStackand center-aligned, it does not need padding.

编辑 2:删除了图像上的填充,因为它导致图像从中心稍微偏移。由于它是自己的HStack并且居中对齐,因此不需要填充。

Edit 3: Thanks to @Chris Prince in the comments, I decided to make a simple NavigationBar-esque custom view that you can provide left, center, and right arguments to create the effect that the OP desired (where each set of views are aligned independently of each other):

编辑 3:感谢@Chris Prince 在评论中,我决定制作一个简单的 NavigationBar 风格的自定义视图,您可以提供左、中和右参数来创建 OP 所需的效果(每组视图都对齐)相互独立):

struct CustomNavBar<Left, Center, Right>: View where Left: View, Center: View, Right: View {
    let left: () -> Left
    let center: () -> Center
    let right: () -> Right
    init(@ViewBuilder left: @escaping () -> Left, @ViewBuilder center: @escaping () -> Center, @ViewBuilder right: @escaping () -> Right) {
        self.left = left
        self.center = center
        self.right = right
    }
    var body: some View {
        ZStack {
            HStack {
                left()
                Spacer()
            }
            center()
            HStack {
                Spacer()
                right()
            }
        }
    }
}

Usage:

用法

struct ContentView: View {
    let buttonSize: CGFloat = 30
    var body: some View {
        VStack {
            CustomNavBar(left: {
                Button(action: {
                    print("Tapped")
                }, label: {
                    Image(systemName: "star")
                        .resizable()
                        .frame(width: self.buttonSize, height: self.buttonSize, alignment: .leading)
                }).padding()
            }, center: {
                Image(systemName: "star")
                    .resizable()
                    .frame(width: 30, height: 30, alignment: .center)
            }, right: {
                HStack {
                    Text("Long text here")
                    Image(systemName: "star")
                        .resizable()
                        .frame(width: 30, height: 30, alignment: .center)
                        .padding(.trailing)
                }.foregroundColor(.red)
            })
            Spacer()
            Text("Normal Content")
            Spacer()
        }
    }
}

Example code shown on simulator

模拟器上显示的示例代码

回答by kishor soneji

You center the view using position property try this code

您使用位置属性居中视图试试这个代码

Group{ // container View
   Image("twitter-logo")
            .resizable()
            .frame(width: 30, height: 30, alignment: .center)
}.position(x: UIScreen.main.bounds.width/2)