SwiftUI开发小技巧总结(不定期更新)
目前SwiftUI还不完善,而且实际使用还会存在一些缺陷。网上的教程目前还很少,有也是收费的。因此特地整理一些平时开发中遇到的问题,免费提供给读者。
(注:本文主要面向对SwiftUI有一定基础的读者。)
调整状态栏样式 StatusBarStyle
尝试Info.plist
和UIApplication.statusBarStyle
方法无效。如果有UIViewController
作为根视图,重写方法preferredStatusBarStyle
,这样可以控制全局;如果要设置单个页面的样式用preferredColorScheme(.light)
,但测试似乎设置无效。还有另一个方法:stackoverflow.com/questions/5…
调整导航栏样式 NavigationBar
let naviAppearance = UINavigationBarAppearance() naviAppearance.configureWithOpaqueBackground() // 不透明背景样式 naviAppearance.backgroundColor = UIColor.whiteColor // 背景色 naviAppearance.shadowColor = UIColor.whiteColor // 阴影色 naviAppearance.titleTextAttributes = [:] // 标题样式 naviAppearance.largeTitleTextAttributes = [:] // 大标题样式 UINavigationBar.appearance().standardAppearance = naviAppearance UINavigationBar.appearance().compactAppearance = naviAppearance UINavigationBar.appearance().scrollEdgeAppearance = naviAppearance复制代码
注意configureWithOpaqueBackground()
需要在其它属性设置之前调用,除此之外还有透明背景configureWithTransparentBackground()
,设置背景模糊效果backgroundEffect()
,背景和阴影图片等,以及导航栏按钮样式也可修改。
调整标签栏样式 TabBar
let itemAppearance = UITabBarItemAppearance() itemAppearance.normal.iconColor = UIColor.whiteColor // 正常状态的图标颜色 itemAppearance.normal.titleTextAttributes = [:] // 正常状态的文字样式 itemAppearance.selected.iconColor = UIColor.whiteColor // 选中状态的图标颜色 itemAppearance.selected.titleTextAttributes = [:] // 选中状态的文字样式 let tabBarAppearance = UITabBarAppearance() tabBarAppearance.configureWithOpaqueBackground() // 不透明背景样式 tabBarAppearance.stackedLayoutAppearance = itemAppearance tabBarAppearance.backgroundColor = UIColor.whiteColor // 背景色 tabBarAppearance.shadowColor = UIColor.clear // 阴影色 UITabBar.appearance().standardAppearance = tabBarAppearance复制代码
注意configureWithOpaqueBackground()
同样需要在其它属性设置之前调用,和UINavigationBarAppearance
一样有同样的设置,除此之外还可以为每个标签项设置指示器外观。
TabView设置默认选中页面
TabView(selection: $selectIndex, content: {})复制代码
同时每个标签项需要设置索引值tag()
键盘
输入框获得焦点(弹出键盘):在iOS15上增加了方法focused()
,注意这个方法在视图初始化时是无效的,需要在onAppear()
中延迟一定时间调用才可以。在此之前的系统只能自定义控件的方法实现参考这个:stackoverflow.com/questions/5…
关闭键盘:UIApplication.shared.keyWindow?.endEditing(true)
添加键盘工具栏:.toolbar()
手势
获得按下和松开的状态:
.simultaneousGesture( DragGesture(minimumDistance: 0) .onChanged({ _ in }) .onEnded({ _ in }) )复制代码
通过代码滚动ScrollView到指定位置:借助ScrollViewReader
可以获取位置,在onAppear()
中设置位置scrollTo()
,我们实际使用发现,需要做个延迟执行才会有效,可以把执行放在DispatchQueue.main.async()
中执行。
通过代码返回上一个视图
开发中会经常遇到这样的需求,列表中选择一项,进入子页面。操作完成自动返回上一页。此时再次点击列表中的某一项,会发现显示的页面内容是错误的。如果你是用NavigationLink
做页面跳转,并传递了isActive
参数,那么是会遇到这样的问题。解决办法是,列表中的每一项不要使用NavigationLink
进行跳转,通过按钮设置当前数据模型,再跳转页面。判断数据模型不为空时,加载NavigationLink
,但让其始终隐藏,因为我们需要借助NavigationLink
才能跳转。代码示例:
if _selectedItem != nil { NavigationLink(destination: FavoriteContentView(content: _selectedItem!, showing: $_isShowingContent), isActive: $_isShowingContent) {}.hidden() } LazyVStack(spacing: 0) { ForEach(_data, id: \.self) { item in FavoriteListRow(item: item, action: { _selectedItem = item _isShowingContent = true }) } }复制代码
如果在导航控制器视图进入子页面遇到TabBar不消失问题:NavigationView
写在TabView
外面就可以了。
NavigationView { TabView {} }复制代码
但是这样会导致新的问题,使用NavigationLink
的isActive
参数通过子页面无法触发返回,这个是苹果的缺陷。解决的办法是NavigationView
写在TabView
内部,TabBar
需自己单独实现一个。
TextEditor修改背景色
UITextView.appearance().backgroundColor复制代码
Text文本内部对齐方式
multilineTextAlignment(.center)复制代码
@State修饰的变量如何在init中初始化
init方法中直接赋值会发现无法成功,应该用State<Type>()
包装起来,例如:
_value = State<Int>(initialValue: 1)复制代码
View如何忽略触摸事件
allowsHitTesting(false)
作者:iOS技术小组
链接:https://juejin.cn/post/7037780197076123685