在 SwiftUI 中重复图案图像作为背景?

Posted

技术标签:

【中文标题】在 SwiftUI 中重复图案图像作为背景?【英文标题】:Repeated pattern image as background in SwiftUI? 【发布时间】:2019-06-10 06:10:44 【问题描述】:

我正在尝试将图像设置为背景并在整个屏幕中重复。在 UIKit 中它就像这行代码一样简单:

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

在 SwiftUI 中是否有等价物?

var body: some View 
  HStack 
    VStack 
      Spacer()
    
    Spacer()
  
  .background(
    Image("background") // Need this pattern image repeated throughout the page
  )

【问题讨论】:

【参考方案1】:

最简单的方法是使用 resizable 修饰符并将调整大小模式设置为Image.ResizingMode.tile

Image("background")
.resizable(resizingMode: .tile)

【讨论】:

以上是关于在 SwiftUI 中重复图案图像作为背景?的主要内容,如果未能解决你的问题,请参考以下文章