简体   繁体   中英

How to display a button on the picture that sees the change of "screen" in SwiftUI

Please tell me how to put a button on top of the picture (picture below), it should be located in the lower right corner of itself. And this button should lead to another screen (navigationview). Thanks in advance !

在此处输入图像描述

For a long time I tried to figure it out on my own, but nothing worked.

You can take reference from this code.

import SwiftUI

struct ContentView: View {
    var body: some View {
            ZStack{
                    Button (action: {})
                    {
                        VStack{
                            ZStack{
                                VStack{
                                    Image("image")
                                        .resizable()
                                        .overlay {
                                            Button {
                                               // any action 
                                            } label: {
                                                Text("Let's Get acquainted")
                                                    .font(.system(size: 50))
                                                    .foregroundColor(.white)
                                                    .bold()
                                                    .position(x: 150, y: 100)
                                            }
                                        }
                                }
                                
                            }
                        }
                    }
            }
        }
}

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

You need to use either a ZStack or .overlay :

  1. Using ZStack :
ZStack(alignment: .bottomTrailing) {
    //your content
    //your image
}
  1. Using .overlay :
//your content
    .overlay(alignment: .bottomTrailing) {
        //your Button
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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