How to set Border for Image in SwiftUI?

SwiftUI Image – Border

To set border for Image, add border() modifier to the Image. You may specify the border color and border width via border().

Example

In this example, we shall display an image from assets in two Image views with different borders. The first Image shall have a border with black color and width of 5, and the second Image shall have a border with orange color and width of 10.

ContentView.swift

import SwiftUI

struct ContentView: View {
    var body: some View {
        Image("sunrise")
            .resizable()
            .aspectRatio(contentMode: .fit)
            .border(Color.black, width: 5)
            .padding(.all, 50)
        Image("sunrise")
            .resizable()
            .aspectRatio(contentMode: .fit)
            .border(Color.orange, width: 10)
            .padding(.all, 50)
    }
}

Screenshot

Set Border for Image in SwiftUI

Summary

Summarising thisĀ SwiftUI Tutorial, we learned how to set border for an Image using SwiftUI.