在SwiftUI中,我如何将一个NavigationLink定位到距离底部一定距离的位置?

huangapple go评论55阅读模式
英文:

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(&quot;SomeImage&quot;)
                .resizable()
                .scaledToFill()
                .padding()

            VStack {
                Spacer() // &lt;--- 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.

huangapple
  • 本文由 发表于 2023年6月5日 01:24:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定