2020年开发笔记第一期
SwiftUI中怎么设置选中图片的颜色?
public func accentColor(_ accentColor: Color?) -> some View
Example
import SwiftUI
/// 自定义的Tab
struct CustomTabView<Content:View>: View {
///需要展示的内容元祖数组
/// - Parameter title: 显示标题
/// - Parameter image: 默认的显示图片
/// - Parameter selectImage: 选中的图片 图片颜色将由`selectColor`设置
/// - Parameter content: 对应的内容试图
let contents:[(title:String, image:UIImage, selectImage:UIImage, content:Content)]
/// 设置选中图片的前景色
let selectColor:UIColor
@State var tabIndex:Int = 0
var body: some View {
TabView(selection: $tabIndex) {
ForEach(0 ..< contents.count) { index in
contents[index].3
.tabItem {
TabItemView(title: contents[index].0,
image: contents[index].1,
selectImage: contents[index].2,
index: index,
currentIndex: $tabIndex)
}
.tag(index)
}
}.accentColor(Color(selectColor))
}
}
extension CustomTabView {
struct TabItemView: View {
let title:String
let image:UIImage
let selectImage:UIImage
let index:Int
@Binding var currentIndex:Int
var body: some View {
VStack{
if (currentIndex == index) {
Image(uiImage: selectImage)
} else {
Image(uiImage: image.withRenderingMode(.alwaysTemplate))
}
Text(title)
}
}
}
}
struct CustomTabView_Previews: PreviewProvider {
static var previews: some View {
CustomTabView(contents: [
("首页",#imageLiteral(resourceName: "icon_nav_food1"),#imageLiteral(resourceName: "icon_nav_food2"),Text("1")),
("首页",#imageLiteral(resourceName: "icon_nav_food1"),#imageLiteral(resourceName: "icon_nav_food2"),Text("2"))
], selectColor: .red)
}
}
Swift使用#if DEBUG
1 第一步在Build Settings->Swift Compiler - Custom Flags->"Other Swift Flags" 在Debug新增"-DDBUG"或者"-D DEBUG"
2 第二部Build Settings->Apple LLVM x.x - Preprocessiong->Preprocessor Macros在Debug新增"DEBUG=1"
Flutter 获取当前Widget所有的子树
对于我们常用的StatefulWidget
和StatelessWidget
我们可以通过下面方法
createElement().debugGetDiagnosticChain()
SwiftUI cornerRadius方法无法给UIViewRepresentable的UIView生成圆角
需要用UIView的方法设置圆角
view.layer.masksToBounds = true
view.layer.cornerRadius = cornerRadius
UIScroolView实现自定义分页
public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
/// 实现自定义分页
if velocity != .zero {
/// 如果velocity不为空 则代表是用户快速拖动离开的 则滚动到下一个位置
/// 获取到当前的索引
guard let currentIndex = currentShopIndex else {
return
}
var index:Int
if velocity.x > 0 {
index = currentIndex + 1
} else {
index = currentIndex - 1
}
index = max(0, index)
index = min(index, self.currentDisplayModels.count - 1)
let point = scrollCellPoint(index: index)
targetContentOffset.pointee = point
}
}
SwiftUI 自定义UINavigationBar背景颜色
struct NavigationBarModifier: ViewModifier {
var backgroundColor: UIColor?
init( backgroundColor: UIColor?) {
self.backgroundColor = backgroundColor
let coloredAppearance = UINavigationBarAppearance()
coloredAppearance.configureWithTransparentBackground()
coloredAppearance.backgroundColor = .clear
coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
UINavigationBar.appearance().standardAppearance = coloredAppearance
UINavigationBar.appearance().compactAppearance = coloredAppearance
UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
UINavigationBar.appearance().tintColor = .white
}
func body(content: Content) -> some View {
ZStack{
content
VStack {
GeometryReader { geometry in
Color(self.backgroundColor ?? .clear)
.frame(height: geometry.safeAreaInsets.top)
.edgesIgnoringSafeArea(.top)
Spacer()
}
}
}
}
}
extension View {
public func navigationBarBackgroundColor(_ backgroundColor: UIColor?) -> some View {
self.modifier(NavigationBarModifier(backgroundColor: backgroundColor))
}
}
SwiftUI .position 定位不准确
因为position是按照中心点进行偏移定位的