【问题标题】:align a bottom constraint of one view to another view in SwiftUI在 SwiftUI 中将一个视图的底部约束与另一个视图对齐
【发布时间】:2021-06-28 04:56:12
【问题描述】:

如何在 SwiftUI 中将一个视图的底部约束与另一个视图对齐? 例如: 我有一个图像和一个文本,我想将文本与图像的底部约束对齐并提供 10 个填充。我该怎么做?

例如:我正在为 ZStack 使用以下代码。文本在图像的中心对齐,填充在中心完成。但我希望它在底部对齐。

ZStack {
        let image = UIImage(named: "blockedEmpty")
        Image(uiImage: image!).resizable().frame(width: 100, height: 100).aspectRatio(contentMode: .fit)
        Text("Image text").offset(CGSize(width: 0.0, height: 10.0))
}

【问题讨论】:

    标签: ios swift iphone swiftui


    【解决方案1】:

    你应该在这里使用overlay:


    import SwiftUI
    
    struct ContentView: View {
        var body: some View {
            
            let image = UIImage(named: "blockedEmpty")
            Image(uiImage: image!).resizable().frame(width: 300, height: 300).aspectRatio(contentMode: .fit)
                .overlay(Text("Image text").padding(), alignment: .bottom)
            
        }
    }
    

    【讨论】:

    • 但我希望文本位于图像上方,底部与图像相同。我可以对文本进行填充,使其在图像底部上方 10 像素处。基本上我需要知道,2 个元素的底部如何相同?
    • @ShivaniBajaj:根据您的需要更新答案
    猜你喜欢
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    相关资源
    最近更新 更多