You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Helper bridge to UIViewController to access enclosing UITabBarController// and thus its UITabBarstructTabBarAccessor:UIViewControllerRepresentable{varcallback:(UITabBar)->VoidprivateletproxyController=ViewController()func makeUIViewController(context:UIViewControllerRepresentableContext<TabBarAccessor>)->UIViewController{
proxyController.callback = callback
return proxyController
}func updateUIViewController(_ uiViewController:UIViewController, context:UIViewControllerRepresentableContext<TabBarAccessor>){}typealiasUIViewControllerType=UIViewController// viewWillAppear 가 탈때 가지고 있는 탭바를 클로저 콜백으로 넘겨준다.privateclassViewController:UIViewController{varcallback:(UITabBar)->Void={ _ in}overridefunc viewWillAppear(_ animated:Bool){
super.viewWillAppear(animated)
if let tabBar =self.tabBarController {self.callback(tabBar.tabBar)}}}}
익스텐션으로 간편하게 쓰도록 만들기
extensionView{/// 탭바 숨김 처리 여부/// - Parameter isHidden:/// - Returns: func setTabBarVisibility(isHidden :Bool)->someView{background(TabBarAccessor{ tabBar inprint(">> TabBar height: \(tabBar.bounds.height)")// !! use as needed, in calculations, @State, etc.// 혹은 높이를 변경한다던지 여러가지 설정들이 가능하다.
tabBar.isHidden = isHidden
})}}
사용 예
// Demo SwiftUI view of usagestructTestTabBar:View{varbody:someView{TabView{Text("First View").setTabBarVisibility(isHidden: true).tabItem{Image(systemName:"1.circle")}.tag(0)Text("Second View").tabItem{Image(systemName:"2.circle")}.tag(1)}}}