Shivani Bajaj
Shivani Bajaj

Reputation: 1046

align a bottom constraint of one view to another view in SwiftUI

How to align a bottom constraint of one view to another view in SwiftUI? For example: I have an image and a text and I want to align text to bottom constraint of the image and give 10 padding. How can I do that?

For example: I am using following code for ZStack. And the Text is aligned at centre of the image and padding is done at centre. But I want it to be aligned at bottom.

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))
}

Upvotes: 1

Views: 1950

Answers (1)

swiftPunk
swiftPunk

Reputation: 1

You should use overlay here:


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)
        
    }
}

Upvotes: 4

Related Questions