简体   繁体   English

在 SwiftUI 中推送新视图时隐藏 TabBar

[英]Hide TabBar when a new view is pushed in SwiftUI

how can I hide the TabBar when a new View is pushed via NavigationLink?当通过 NavigationLink 推送新视图时,如何隐藏 TabBar?

Here's how I push the next View:这是我推送下一个视图的方式:

TabView {
    NavigationView {
        List(fakeUser) { user in
            NavigationLink(destination: ChatDetailView(user: user)) {
                ChatCell(user: user)
            }
        }
        .navigationBarTitle("Chats")
        .navigationBarItems(leading: leadingBarItem, trailing: trailingBarItem)
    }
    .tabItem {
        Image(systemName: "message.fill")
            .font(.system(size: 20))
        Text("Chats")
    }
}

Caution: rise exception on Xcode 11.2/iOS 13.2注意:在 Xcode 11.2/iOS 13.2 上出现异常

Here is a relayout which gives an effect you requested, as far as I understood.据我了解,这是一个重新布局,可提供您要求的效果。

However, although there is nothing criminal in below code, on navigate back internals of UIKit got into exception:然而,虽然在下面的代码中没有犯罪,但在 UIKit 的内部导航返回异常:

 2019-11-24 10:54:36.644037+0200 Test[1180:41920] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.' 2019-11-24 10:54:36.644037+0200 测试 [1180:41920] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“试图弹出到不存在的视图 controller。” *** First throw call stack: ( 0 CoreFoundation 0x00007fff23c4f02e __exceptionPreprocess + 350 1 libobjc.A.dylib 0x00007fff50b97b20 objc_exception_throw + 48 2 CoreFoundation 0x00007fff23c4eda8 +[NSException raise:format:arguments:] + 88 3 Foundation 0x00007fff256c9b61 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191 4 UIKitCore 0x00007fff4713d9d1 __57-[UINavigationController popToViewController:transition:]_block_invoke + 620 *** First throw call stack: ( 0 CoreFoundation 0x00007fff23c4f02e __exceptionPreprocess + 350 1 libobjc.A.dylib 0x00007fff50b97b20 objc_exception_throw + 48 2 CoreFoundation 0x00007fff23c4eda8 +[NSException raise:format:arguments:] + 88 3 Foundation 0x00007fff256c9b61 -[NSAssertionHandler handleFailureInMethod:object:文件:行号:描述:] + 191 4 UIKitCore 0x00007fff4713d9d1 __57-[UINavigationController popToViewController:transition:]_block_invoke + 620

Approach code方法代码

var body: some View {
    NavigationView {
        TabView {
            List(fakeUser) { user in
                NavigationLink(destination: ChatDetailView(user: user)) {
                    ChatCell(user: user)
                }
            }
            .navigationBarTitle("Chats")
            .navigationBarItems(leading: leadingBarItem, trailing: trailingBarItem)
            .tabItem {
                Image(systemName: "message.fill")
                    .font(.system(size: 20))
                Text("Chats")
            }
        }
        .navigationBarTitle("Chats")
    }
}

In iOS 16 you have this option:在 iOS 16 中你有这个选项:

.toolbar(.hidden, for: .tabBar)

Just attach it to the view which is going to be pushed.只需将其附加到将要推送的视图即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当按下任何视图时,iphone + hide标签栏 - iphone+hide tabbar when any view is pushed 在 flutter 中推送新页面时如何隐藏原生 Tabbar? - how to hide native Tabbar when a new page gets pushed in flutter? 在 SwiftUI 中使用 NavigationLink 导航时如何隐藏 TabBar? - How to hide the TabBar when navigate with NavigationLink in SwiftUI? 当前一个视图搜索栏处于活动状态时,SwiftUI 无法在推送视图上隐藏导航栏 - SwiftUI can't hide navigationBar on pushed view when previous view search bar active 将Tabbar推入UITabBarController内部的ViewController时不会隐藏 - Tabbar won't hide when pushed into a ViewController inside a UITabBarController 隐藏 TabBar 时在视图中设置工具栏 position - Set Toolbar position in View when hide the TabBar 隐藏和取消隐藏标签栏时管理视图框架 - Managing view frame when hide and unhide a tabbar 推送视图时如何隐藏tabBar? - How to hide the tabBar when push a view? UISearchController按下时不会隐藏视图 - UISearchController doesn't hide view when pushed 推入导航堆栈时隐藏 TabBar 并在弹出导航堆栈时将其带回来 - Hide TabBar when pushed into the navigation stack and bring it back when popped out of the navigation stack
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM