【问题标题】:How to make a button in front of an image but constrained to the left bottom and right of the view in SwiftUI?如何在图像前面制作一个按钮,但限制在 SwiftUI 中视图的左下角和右下角?
【发布时间】:2021-04-24 02:47:05
【问题描述】:

如何使登录视图展开,使其左侧被限制在左侧,而右侧也被限制在右侧?


import SwiftUI

struct Intro: View {
    var body: some View {
        ZStack {
            Image("IntroImage")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(alignment: .center)
            VStack {
                Spacer()
                Button(action: {}, label: {
                    Text("Sign in")
                        .foregroundColor(Color.black)
                        .padding()
                        .background(Color.green)
                })
            }
        }
        .ignoresSafeArea()
    }
}

struct Intro_Previews: PreviewProvider {
    static var previews: some View {
        Intro()
    }
}

我希望它看起来像这样:

我使用的是 640 × 1136 png 的图像。我正在包括 iPhone 11 在内的所有设备上对其进行测试。

【问题讨论】:

    标签: swift swiftui combine


    【解决方案1】:

    frame(maxWidth: .infinity) 添加到您的ButtonText

    Button(action: {}, label: {
                        Text("Sign in")
                            .frame(maxWidth: .infinity)
                            .foregroundColor(Color.black)
                            .padding()
                            .background(Color.green)
    })
    

    iPhone 11 上的外观:

    【讨论】:

    • 这里发布了确凿的事实。
    • 在我的例子中,这使得视图超出了右侧的超级视图,但不是在左侧,从而使按钮偏向右侧。
    • 您能否编辑您的问题以包含重现该问题所需的内容?
    • 完成,图片尺寸和 iPhone 列在底部。
    • 除了您列出的代码之外,什么都不会发生?我已经用 iPhone 11 的外观编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    相关资源
    最近更新 更多