日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

不刷新页面的tab_SwiftUI小技巧之如何解决Tab切换后页面重置和List刷新bug

發(fā)布時(shí)間:2024/9/19 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 不刷新页面的tab_SwiftUI小技巧之如何解决Tab切换后页面重置和List刷新bug 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
SwiftUI的tabview非常好用,簡單幾行就能構(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、測試頁面

import SwiftUIstruct ExampleView: View {@State var text: String = ""var body: some View {UIKitTabView([UIKitTabView.Tab(view: NavView(), title: "首頁", image: "phone32.png"),UIKitTabView.Tab(view: Text("Second View"), title: "其他", image: "")])} }struct NavView: View {var body: some View {NavigationView {VStack {NavigationLink(destination: Text("您能看到這個(gè)頁面,則證明bug修復(fù)")) {Text("顯示詳細(xì)頁")}}}} }

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

  • 請關(guān)注我的專欄icloudend, SwiftUI教程與源碼 https://www.jianshu.com/c/7b3e3b671970

總結(jié)

以上是生活随笔為你收集整理的不刷新页面的tab_SwiftUI小技巧之如何解决Tab切换后页面重置和List刷新bug的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。