2020-11-05 SwiftUI + widget

1. 相关地址

苹果官方swiftUI教程:https://developer.apple.com/tutorials/swiftui/creating-and-combining-views

2. 设置随机颜色

Color(UIColor.init(
          red:CGFloat(
              arc4random_uniform(255))/CGFloat(255.0), 
              green:CGFloat(arc4random_uniform(255))/CGFloat(255.0), 
              blue:CGFloat(arc4random_uniform(255))/CGFloat(255.0) ,
              alpha: 1
          )
)

3. SwiftUI widget 九宫格布局(表格) 一行四个,两行

import SwiftUI
import WidgetKit

struct CaseStatGrid: View {
  
  let totalCount: TotalCaseCount
  let columns: [GridItem] = Array(repeating: .init(.flexible(), spacing: 0), count: 3)
  
  var body: some View {
      VStack {
          GeometryReader { proxy in
              LazyVGrid(columns: columns, spacing: 0) {
                  CaseStatView(text: "Confirmed", totalCountText: totalCount.confirmedText, color: confirmedColor, height: proxy.size.height / 4)
                  CaseStatView(text: "Deaths", totalCountText: totalCount.deathText, color: deathColor, height: proxy.size.height / 4)
                  CaseStatView(text: "Sick", totalCountText: totalCount.sickText, color: sickColor, height: proxy.size.height / 4)
                  CaseStatView(text: "Recovered", totalCountText: totalCount.recoveredText, color: recoveredColor, height: proxy.size.height / 4)
//                    CaseStatView(text: "Sick", totalCountText: totalCount.sickText, color: sickColor, height: proxy.size.height / 2)
//                    CaseStatView(text: "Recovered", totalCountText: totalCount.recoveredText, color: recoveredColor, height: proxy.size.height / 2)
              }
          }
      }
  }
}

struct CaseStatGrid_Previews: PreviewProvider {
  static var previews: some View {
      CaseStatGrid(totalCount: .init(title: "USA", confirmed: 300, death: 100, recovered: 100))
          .previewContext(WidgetPreviewContext(family: .systemMedium))
  }
}

4. swiftUI widget 设置图片背景(去掉白边)


## 方法1
VStack(alignment: .center,spacing: 30){

            }.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)

            .background(
                Image("pyq-1")
                    .resizable()
                    .scaledToFill()
)

## 方法2
GeometryReader() { geometry in
                       VStack {
                           Text("222")
                       }
            }
            .background(
                Image("pyq-1")
                    .resizable()
                    .scaledToFill()
            )

5.swiftUI 计时 :

#在widget中不好使
https://www.jianshu.com/p/29683d7b0dcf  

6.设置控自动圆角纯色背景

ContainerRelativeShap().fill(Color.red)

你可能感兴趣的:(2020-11-05 SwiftUI + widget)