resizable() 自适应大小 * 图片没有完全显示全,我们可以用Image的resizable()来让图片自动适应
.aspectRatio(contentMode: .fit) 设置图片的显示模式
Identifiable主要作用就是作为一个对象的唯一标识。能够与List一起列出。List可以像UITableViewController一样显示可标识集合中的数据列表
.frame(minWidth: 0, maxWidth: .infinity) 设置控件铺满整个屏幕
SwiftUI 有两种载入图片方式
出现No image named found in asset catalog for main bundle问题通常是没有正确使用好载入方式
clipped() 将View裁剪成矩形 默认情况下,视图的边界框架仅用于布局,因此超出框架边缘的任何内容仍然可见。使用clipped(antialiased:)修饰器可以隐藏超出这些边缘的任何内容。
UIScreen.main.bounds.width 获取屏幕大小
.resizable() .scaledToFit() 照片大小适中 Image("circle") .resizable() .scaledToFit() .frame(minWidth:nil, idealWidth: nil, maxWidth: UIScreen.main.bounds.width, minHeight: nil, idealHeight: nil, maxHeight: 300, alignment: .center )
自定义上下左右任意圆角位置和度数 ` struct RoundedCorners: View { var color: Color = .blue var tl: CGFloat = 0.0 var tr: CGFloat = 0.0 var bl: CGFloat = 0.0 var br: CGFloat = 0.0
var body: some View { GeometryReader { geometry in Path { path in
let w = geometry.size.width
let h = geometry.size.height
// Make sure we do not exceed the size of the rectangle
let tr = min(min(self.tr, h/2), w/2)
let tl = min(min(self.tl, h/2), w/2)
let bl = min(min(self.bl, h/2), w/2)
let br = min(min(self.br, h/2), w/2)
path.move(to: CGPoint(x: w / 2.0, y: 0))
path.addLine(to: CGPoint(x: w - tr, y: 0))
path.addArc(center: CGPoint(x: w - tr, y: tr), radius: tr, startAngle: Angle(degrees: -90), endAngle: Angle(degrees: 0), clockwise: false)
path.addLine(to: CGPoint(x: w, y: h - br))
path.addArc(center: CGPoint(x: w - br, y: h - br), radius: br, startAngle: Angle(degrees: 0), endAngle: Angle(degrees: 90), clockwise: false)
path.addLine(to: CGPoint(x: bl, y: h))
path.addArc(center: CGPoint(x: bl, y: h - bl), radius: bl, startAngle: Angle(degrees: 90), endAngle: Angle(degrees: 180), clockwise: false)
path.addLine(to: CGPoint(x: 0, y: tl))
path.addArc(center: CGPoint(x: tl, y: tl), radius: tl, startAngle: Angle(degrees: 180), endAngle: Angle(degrees: 270), clockwise: false)
}
.fill(self.color)
}
} } `
Decodable 能够从JSON文件中对其进行解码
swiftUI多选https://stackoverflow.com/questions/56706188/how-does-one-enable-selections-in-swiftuis-list
SwiftUI 制作内容类App的一般流程 SwiftUI基本思想
数据驱动 创建个struct 用于结构化存储对象 创建个读写数据的data.swift 界面显示
mutating 类是引用类型,而结构和枚举是值类型。这意味着类对象共享该对象的单个实例,并在传递给任何函数或新对象时传递相同的引用,而值类型是创建其副本并仅传递值的类型 值类型,我们不能够直接修改属性 在值类型中向任何函数添加突变关键字可以使它们能够修改变量。在内部,当我们尝试突变值类型时,它不会mutate其值,但它会mutate持有该值的变量。
RandomAccessCollection 支持高效随机访问的集合
Spacer()可以将Stack撑开铺满全屏
edgesIgnoringSafeArea 忽略安全区域留海和底部
容器都是有10个对象的限制 VStack HStack ZStack Group List
获得struct名称的两种方式 print(String(reflecting: a.self)) print("(a.self)")
泛型占位常用字符T(MyTypeParameter),U和V表面是占位符不是值
超长文本可以有两种解决方案 直接省略 .scaledToFit() .lineLimit(nil) 自动缩小换行 minimumScaleFactor(0.3)
fixedSize() -> View 将视图固定为理想尺寸。
fixedSize(horizontal: Bool, vertical: Bool) -> View 将视图固定为指定尺寸的理想尺寸。
func listRowInsets(EdgeInsets?) -> View 设置插入列表时要应用于视图的插入占位填充。
func listRowBackground(V?) -> View 当放置在列表中时,在视图后面设置一个视图。
func listRowPlatterColor(Color?) -> View 设置当放置在列表中时应用于视图系统单元盘的颜色。
alignment: 布局对齐格式, 默认为 .center
spacing: 子 View 的间距, 系统默认为 8
如何把 View 分到 左右两边
renderingMode 图片渲染样式,一般设置原始样式original
layoutPriority 设置父级布局应将空间分配给子级的优先级。默认优先级为0。在一组同级视图中,提高视图的布局优先级会鼓励该视图在缩小组时缩小,而在拉伸组时更快。当空间不够的情况下,优先级越高,展示的越完善,其他同级的子控件会压缩空间显示
position(CGPoint) -视图的中心点设置 将视图的中心固定在其父级坐标空间中的指定点 position(x: CGFloat, y: CGFloat) -> View 将视图的中心固定在其父级坐标空间中的指定坐标处
offset(x: CGFloat, y: CGFloat) 使视图偏移指定的水平和垂直距离
edgesIgnoringSafeArea(Edge.Set) 将视图扩展到指定边缘的安全区域之外
List的子视图上添加Button,Button事件会覆盖整个子视图,也就是List中cell的点击事件也会触发Button点击事件
SwiftUI 中使用 UIView 子类,需要将其他 view 包装在遵循 UIViewRepresentable 协议
UIViewRepresentable 协议需要实现两个方法:
可以用这个 coordinator 实现常见的 Cocoa 模式,例如代理、数据源以及通过 target-action 响应用户事件。SwiftUI 会在调用 makeUIViewController(context:) 方法之前调用 makeCoordinator() 方法,这样配置 view controller 时,我们可以访问 coordinator 对象。
truncationMode 太长而无法容纳可用空间的文本行设置截断模式
ViewModifier视图修改器,可以用于将视图布局修饰样式抽离封装 func modifier(T) -> ModifiedContent<Self, T> 将修改器应用于视图。
struct SearchBarTextModifier: ViewModifier {
func body(content: Content) -> some View {
content
.frame(height:40)
.padding(EdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8))
.border(Color.gray,width:4)
.cornerRadius(8)
.padding(EdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 4))
}
}
TextField("Search",text: $searchStr)
.modifier(SearchBarTextModifier())
原文:https://www.cnblogs.com/liuxiaokun/p/12676966.html