首页 > 解决方案 > 使用全屏图像和导航查看链接

问题描述

我是 swiftui 的新手,并且正在为一些基本的东西而苦苦挣扎,我正在尝试在按钮上创建一个带有 2 个按钮的视图。我想要一个图像作为整个视图的背景。

var body: some View {
        ZStack {
            Image("bg-welcome").edgesIgnoringSafeArea(.all)
        }
    }

这很好用我这样设置我的视图

NavigationView {
            ZStack {

                VStack(alignment: HorizontalAlignment.leading, spacing: 10) {
                    Text("1").foregroundColor(.white)
                    Text("2").foregroundColor(.white)
                    Spacer()
                    Button(action: {}){
                        Text("ooooooooo")
                    }
                }.background(Image("bg-welcome"))
            }

        }

现在我有两个问题: - 在背景图像之前显示一个空白 - 第一个文本被推到下面,肯定是在为导航栏保留的空间之后,我不想要它,因为当我导航到下一个屏幕时,空间仍然被占用,我想要整个可用的屏幕

谢谢您的帮助

标签: iosswiftswiftui

解决方案


尝试这个:

var body: some View {
        NavigationView {
            ZStack {

                VStack(alignment: HorizontalAlignment.leading, spacing: 10) {
                    Text("1").foregroundColor(.white)
                    Text("2").foregroundColor(.white)
                    Spacer()
                    Button(action: {}){
                        Text("Button 1")
                    }
                    Button(action: {}){
                        Text("Button 2")
                    }
                }.background(Image("remachine_poster")
                    .resizable()
                    .aspectRatio(contentMode: .fill)).edgesIgnoringSafeArea(.all)
            }
        }
    }

推荐阅读