英文:
In SwiftUI, how can I position a NavigationLink a distance from the bottom
问题
Here's the translated code portion:
struct SomScreen: View {
    var body: some View {
        ZStack{
            Image("SomeImage")
                .resizable()
                .scaledToFill()
                .padding()
            VStack {
                NavigationLink(destination: SomeNextScreen(), label: {
                    buttonToGetToNextScreen()
                })
            }
        }   
    }
}
To position buttonToGetToNextScreen at the bottom of the VStack, you can use the .padding(.bottom) modifier to add some spacing at the bottom:
struct SomScreen: View {
    var body: some View {
        ZStack{
            Image("SomeImage")
                .resizable()
                .scaledToFill()
                .padding()
            VStack {
                Spacer() // Add a spacer to push the button to the bottom
                NavigationLink(destination: SomeNextScreen(), label: {
                    buttonToGetToNextScreen()
                })
            }
        }   
    }
}
If you remove the VStack, you can position it at the bottom of the screen by using .padding(.bottom) directly on the button or enclosing the entire ZStack in a VStack and adding .frame(maxHeight: .infinity) to the ZStack to make it take up the available vertical space.
英文:
Example of similar code I am using:
struct SomScreen: View {
    var body: some View {
        ZStack{
            Image("SomeImage")
                .resizable()
                .scaledToFill()
                .padding()
            VStack {
                NavigationLink(destination: SomeNextScreen(), label: {
                    buttonToGetToNextScreen()
               })
           }
      }   
   }
}
My buttonToGetToNextScreen ends up in the middle of the VStack. Is there a way to have if positioned from a distance from the bottom of the VStack? Or if I remove the VStack, bottom of the screen?
答案1
得分: 1
你可以使用 Spacer() 将内容推到 VStack 底部:
struct SomScreen: View {
    var body: some View {
        ZStack{
            Image("SomeImage")
                .resizable()
                .scaledToFill()
                .padding()
            VStack {
                Spacer() // <--- 这里
                NavigationLink(destination: SomeNextScreen(), label: {
                    buttonToGetToNextScreen()
               })
           }
      }   
   }
}
英文:
You can use a Spacer() to push the content to the bottom of the VStack:
    struct SomScreen: View {
    var body: some View {
        ZStack{
            Image("SomeImage")
                .resizable()
                .scaledToFill()
                .padding()
            VStack {
                Spacer() // <--- Here
                NavigationLink(destination: SomeNextScreen(), label: {
                    buttonToGetToNextScreen()
               })
           }
      }   
   }
}
答案2
得分: 0
按照 @nickreps 建议的方式,文档 描述了 Spacer() 方法以及如何使用它。
英文:
As suggested by @nickreps 
Documentation
/
SwiftUI
/
Layout fundamentals
/
Spacer describes the Spacer() method and how it can be used.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论