不刷新页面的tab_SwiftUI小技巧之如何解决Tab切换后页面重置和List刷新bug
生活随笔
收集整理的這篇文章主要介紹了
不刷新页面的tab_SwiftUI小技巧之如何解决Tab切换后页面重置和List刷新bug
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
SwiftUI的tabview非常好用,簡(jiǎn)單幾行就能構(gòu)建一套基于Tab的App。但是SwiftUI目前tabview存在一個(gè)小bug,當(dāng)tab切換時(shí)內(nèi)部中的list或NavigationLink將會(huì)重新刷新。
看看bug
當(dāng)我們切換到新的tab時(shí),第一個(gè)tab被重置回初始狀態(tài)了。
這個(gè)bug可以修復(fù)么
我們有兩種解決方案,一種等6月份等WWDC新版發(fā)布,修復(fù)這個(gè)bug。另外就是自己動(dòng)手封裝一個(gè)tabview
實(shí)戰(zhàn)解決
1、測(cè)試頁(yè)面
import SwiftUIstruct ExampleView: View {@State var text: String = ""var body: some View {UIKitTabView([UIKitTabView.Tab(view: NavView(), title: "首頁(yè)", image: "phone32.png"),UIKitTabView.Tab(view: Text("Second View"), title: "其他", image: "")])} }struct NavView: View {var body: some View {NavigationView {VStack {NavigationLink(destination: Text("您能看到這個(gè)頁(yè)面,則證明bug修復(fù)")) {Text("顯示詳細(xì)頁(yè)")}}}} }2、封裝個(gè)UIkitTabview
struct UIKitTabView: View {var viewControllers: [UIHostingController<AnyView>]@State var selectedIndex: Int = 0init(_ views: [Tab]) {self.viewControllers = views.map {let host = UIHostingController(rootView: $0.view)host.tabBarItem = $0.barItemreturn host}}var body: some View {TabBarController(controllers: viewControllers, selectedIndex: $selectedIndex).edgesIgnoringSafeArea(.all)}struct Tab {var view: AnyViewvar barItem: UITabBarIteminit<V: View>(view: V, barItem: UITabBarItem) {self.view = AnyView(view)self.barItem = barItem}// convenienceinit<V: View>(view: V, title: String?, image: String, selectedImage: String? = nil) {let selectedImage = selectedImage != nil ? UIImage(named: selectedImage!) : nillet barItem = UITabBarItem(title: title, image: UIImage(named: image), selectedImage: selectedImage)self.init(view: view, barItem: barItem)}} }3、制作一個(gè)TabBarController
import SwiftUI import UIKitstruct TabBarController: UIViewControllerRepresentable {var controllers: [UIViewController]@Binding var selectedIndex: Intfunc makeUIViewController(context: Context) -> UITabBarController {let tabBarController = UITabBarController()tabBarController.viewControllers = controllerstabBarController.delegate = context.coordinatortabBarController.selectedIndex = 0return tabBarController}func updateUIViewController(_ tabBarController: UITabBarController, context: Context) {tabBarController.selectedIndex = selectedIndex}func makeCoordinator() -> Coordinator {Coordinator(self)}class Coordinator: NSObject, UITabBarControllerDelegate {var parent: TabBarControllerinit(_ tabBarController: TabBarController) {self.parent = tabBarController}func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {parent.selectedIndex = tabBarController.selectedIndex}} }技術(shù)交流
QQ:3365059189 SwiftUI技術(shù)交流QQ群:518696470
- 請(qǐng)關(guān)注我的專欄icloudend, SwiftUI教程與源碼 https://www.jianshu.com/c/7b3e3b671970
總結(jié)
以上是生活随笔為你收集整理的不刷新页面的tab_SwiftUI小技巧之如何解决Tab切换后页面重置和List刷新bug的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 创建多线程_你真的了解多线程吗?
- 下一篇: python 任务调度 celery_p