你好,我已经在快速用户界面中创建了一个自定义选项卡栏,其中包含6个选项卡,每个选项卡都嵌入在一个虚化视图中.当我 Select 第五个或第六个选项卡时,我看到顶部有一个"更多"按钮.这几乎就像导航链接的后退按钮.我怎么才能go 掉这个呢?我试着使用导航栏按钮隐藏(真的),但不起作用.我还try 在我的tabView上添加.bar(.idden,意为:.tabbar),但同样不起作用.如果能帮我解开那个按钮,我将不胜感激.谢谢.

import SwiftUI
import Combine
import MapKit

struct MainTabView: View {
    @State private var selectedTab = 0
    @EnvironmentObject var pop: PopToRoot
    @Namespace var animation

init() {
    UITabBar.appearance().isHidden = true
}
    var body: some View {
        TabView(selection: $selectedTab){
            NavigationView(){
                FeedView()
            }.tag(1)
            NavigationView(){
                JobsView()
            }.tag(2)
            NavigationView(){
                ExploreView()
            }.tag(3)
            NavigationView(){
                QuestionView()
            }.tag(4)
            NavigationView(){
                MessagesHomeView()
            }.tag(5)
            NavigationStack {
                ProfileView()
            }.tag(6)
        }
        .overlay (
            HStack {
                TabBarButton(change: $selectedTab, title: "Home", index: 1, image: selectedTab == 1 ? "h.circle.fill" : "h.circle", animation: animation)
                TabBarButton(change: $selectedTab, title: "Jobs/Shop", index: 2, image: "", animation: animation)
                TabBarButton(change: $selectedTab, title: "Explore", index: 3, image: "magnifyingglass", animation: animation)
                TabBarButton(change: $selectedTab, title: "Ask", index: 4, image: "questionmark", animation: animation)
                TabBarButton(change: $selectedTab, title: "Messages", index: 5, image: selectedTab == 5 ? "message.fill" : "message", animation: animation)
                TabBarButton(change: $selectedTab, title: "Profile", index: 6, image: selectedTab == 6 ? "person.crop.circle.fill" : "person.crop.circle", animation: animation)
            }
            , alignment: .bottom
        )
    }
}

struct TabBarButton: View {
    @Binding var change: Int
    var title: String
    var index: Int
    var image: String
    var animation: Namespace.ID
    @EnvironmentObject var popRoot: PopToRoot
    var body: some View{
        Button {
            change = index
            withAnimation {
                popRoot.tab = index
            }
        } label: {
            VStack {
                Image(systemName: image).font(.title2)
                Text(title).font(.system(size: 10))
            }
            .foregroundColor(popRoot.tab == index ? .orange : .primary.opacity(0.5))
            .frame(maxWidth: .infinity)
            .overlay (
                ZStack {
                    if popRoot.tab == index {
                        TabIndicator()
                            .fill(.orange.gradient)
                            .matchedGeometryEffect(id: "TAB", in: animation)
                            .padding(.top, -9)
                            .padding(.horizontal, 8)
                    }
                }
            )
        }
    }
}

struct TabIndicator: Shape {
    func path(in rect: CGRect) -> Path {
        let newRect = CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.width, height: 3)
        return Path { path in
            path.addRect(newRect)
        }
    }
}

推荐答案

如果您可以用空视图替换工具栏(其中有"More"按钮),请首先选中.

首先,定义一个自定义扩展或视图,可能如下所示:

extension View {
    static var hidden: some View {
        EmptyView()
    }
}

然后,由于navigationBarItems(leading:trailing:)已弃用,因此使用toolbar(content:)与[navigationBarLeading]https://developer.apple.com/documentation/swiftui/toolbaritemplacement/navigationbarleading)或navigationBarTrailing放在一起.

struct MainTabView: View {
    @State private var selectedTab = 0
    @EnvironmentObject var pop: PopToRoot
    @Namespace var animation

    init() {
        UITabBar.appearance().isHidden = true
    }
    
    var body: some View {
        TabView(selection: $selectedTab){
            NavigationView(){
                FeedView()
            }
            .tag(1)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) { EmptyView() }
            }

            NavigationView(){
                JobsView()
            }
            .tag(2)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) { EmptyView() }
            }

            NavigationView(){
                ExploreView()
            }
            .tag(3)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) { EmptyView() }
            }

            NavigationView(){
                QuestionView()
            }
            .tag(4)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) { EmptyView() }
            }

            NavigationView(){
                MessagesHomeView()
            }
            .tag(5)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) { EmptyView() }
            }

            NavigationStack {
                ProfileView()
            }
            .tag(6)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) { EmptyView() }
            }
        }
        .overlay (
            HStack {
                TabBarButton(change: $selectedTab, title: "Home", index: 1, image: selectedTab == 1 ? "h.circle.fill" : "h.circle", animation: animation)
                TabBarButton(change: $selectedTab, title: "Jobs/Shop", index: 2, image: "", animation: animation)
                TabBarButton(change: $selectedTab, title: "Explore", index: 3, image: "magnifyingglass", animation: animation)
                TabBarButton(change: $selectedTab, title: "Ask", index: 4, image: "questionmark", animation: animation)
                TabBarButton(change: $selectedTab, title: "Messages", index: 5, image: selectedTab == 5 ? "message.fill" : "message", animation: animation)
                TabBarButton(change: $selectedTab, title: "Profile", index: 6, image: selectedTab == 6 ? "person.crop.circle.fill" : "person.crop.circle", animation: animation)
            }
            , alignment: .bottom
        )
    }
}

toolbar(content:)修饰符 for each NavigationView在导航栏的前导位置放置EmptyView(),其效果应该是移除导航栏上的"More"按钮或任何其他前导项目.


但这并没有奏效.我仍然可以看到屏幕顶部的More按钮.有什么 idea 吗?

另一种方法是,因为您已经在使用自定义选项卡栏UI,所以完全绕过TabView来使用自定义选项卡栏.

You could hold all the views in an array and show the currently selected one using a conditional statement.
And then use your custom tab bar UI to change the currently selected view.

import SwiftUI
import Combine
import MapKit

struct MainTabView: View {
    @State private var selectedTab = 0
    @EnvironmentObject var pop: PopToRoot
    @Namespace var animation
    
    private var tabs: [AnyView] = [
        AnyView(NavigationView() { FeedView() }),
        AnyView(NavigationView() { JobsView() }),
        AnyView(NavigationView() { ExploreView() }),
        AnyView(NavigationView() { QuestionView() }),
        AnyView(NavigationView() { MessagesHomeView() }),
        AnyView(NavigationStack { ProfileView() })
    ]

    var body: some View {
        VStack {
            if selectedTab < tabs.count {
                tabs[selectedTab]
            }
            
            Spacer()
            
            HStack {
                TabBarButton(change: $selectedTab, title: "Home", index: 0, image: selectedTab == 0 ? "h.circle.fill" : "h.circle", animation: animation)
                // Your other TabBarButtons
            }
            .background(Color.white.shadow(radius: 2))
        }
    }
}

// Your other structures

这样,您就可以绕过默认行为TabView,并使用自定义选项卡栏控制所显示的视图.

Ios相关问答推荐

我想在expo 中使用useSafeAreaInsets来获取paddingbottom,但我得到了一个错误

与iPadOS中带有扣件的模式相似的组件是什么?

由于代码签名时出错,无法在iOS设备上发布iOS Maui Build

为什么iOS委派在KMM中不能正常工作?

归档时命令 PhaseScriptExecution 失败,退出代码非零

实现 ListView Builder Widget Flutter 后退按钮不起作用

使用 Apollo 发布 iOS 应用程序时缺少推送通知权利

撤销 Apple 登录令牌以进行帐户删除过程

clipShape swift的三元

来自 c++ 的runtime_error未在 iOS 中捕获

Swift如何表示单位转换的标准大气压

Xcode intellisense 彩色框中字母的含义,如 f、T、C、M、P、C、K、# 等

如何设置imageView的圆角半径?

如何将 iPhone OSStatus 代码转换为有用的东西?

Xcode 8:函数类型不能有参数标签 destruct 我的构建

以编程方式 Select UITextField 中的所有文本

如何在 UILabel 中找到文本子字符串的 CGRect?

在 iOS 上没有显示数字键盘

打开 XIB 文件后 Xcode 6.3 冻结/挂起

在两个或多个 iPhone 应用程序之间共享数据