Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeated pattern image as background in SwiftUI?

Tags:

swift

swiftui

I'm trying to set an image as background and repeat it throughout the screen. In UIKit it is as simple as this single line of code:

view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png"))

Is there an equivalent in SwiftUI?

var body: some View {
  HStack {
    VStack {
      Spacer()
    }
    Spacer()
  }
  .background(
    Image("background") // Need this pattern image repeated throughout the page
  )
}
like image 469
M Reza Avatar asked Nov 28 '22 21:11

M Reza


1 Answers

The easiest way is to use the resizable modifier and set the resizing mode to Image.ResizingMode.tile.

Image("background")
    .resizable(resizingMode: .tile)
like image 138
Marcio Avatar answered Dec 09 '22 19:12

Marcio