我只想要一条位于tabView中心的底部曲线,但我无法访问tabView形状属性.

Final Image

这就是我想要的.

注:

曲线应始终保持在中心位置.并且项目应该互换,这在给定的代码中已经实现.

import SwiftUI

struct DashboardTabBarView: View {

@State private var selection: String = "home"

struct Item {
    let title: String
    let color: Color
    let icon: String
}

@State var items = [
    Item(title: "cart", color: .red, icon: "cart"),
    Item(title: "home", color: .blue, icon: "house"),
    Item(title: "car", color: .green, icon: "car"),
]

var body: some View {

    TabView(selection: $selection) {
        ForEach(items, id: \.title) { item in // << dynamically !!
            item.color
                .tabItem {
                    Image(systemName: item.icon)
                    Text(item.title)
                }
        }
    }
    .onChange(of: selection) { title in // << reorder with centered item
        let target = 1
        if var i = items.firstIndex(where: { $0.title == title }) {
            if i > target {
                i += 1
            }
            items.move(fromOffsets: IndexSet(integer: target), toOffset: i)
        }
    }
}
}

推荐答案

好的,实际上我们在这里需要解决两个问题,第一-找到标签栏的高度,第二-正确地将视图自定义视图与表示的选定项对齐而不是标准的标签栏.其他的都是mechanics美元.

下面是简化的演示.使用Xcode 14/iOS 16进行测试.

demo

主要部分:

  • 第一个问题的可能解决方案
struct TabContent<V: View>: View {
    @Binding var height: CGFloat
    @ViewBuilder var content: () -> V

    var body: some View {
        GeometryReader { gp in  // << read bottom edge !!
            content()
                .onAppear {
                    height = gp.safeAreaInsets.bottom
                }
                .onChange(of: gp.size) { _ in
                    height = gp.safeAreaInsets.bottom
                }
        }
    }
}
  • 问题2的可能解决方案
    // Just put customisation in z-ordered over TabView
    ZStack(alignment: .bottom) {
        TabView(selection: $selection) {
            // .. content here
        }

        TabSelection(height: tbHeight, item: selected)
    }


struct TabSelection: View {
    let height: CGFloat
    let item: Item
    
    var body: some View {
        VStack {
            Spacer()
            Curve()    // put curve over tab bar !!
                .frame(maxWidth: .infinity, maxHeight: height)
                .foregroundColor(item.color)
        }
        .ignoresSafeArea()   // << push to bottom !!
        .overlay(
            // Draw overlay
            Circle().foregroundColor(.black)
                .frame(height: height).aspectRatio(contentMode: .fit)
                .shadow(radius: 4)
                .overlay(Image(systemName: item.icon)
                    .font(.title)
                    .foregroundColor(.white))
            , alignment: .bottom)
    }
}

Test module is here

Ios相关问答推荐

如何在Flutter 中为S家长增加一个相对高度的间距

objc[25442]:Swift类的扩展和类别不允许有+load方法

视图未更新以在按下按钮时显示另一个视图

执行devicectl:ProcessException时出错:进程异常退出:错误:命令超时超过5.0秒

swiftui动画如何实时确定不对称过渡

在 Swift 中,对象如何被准确地从内存中移除?

CGPath intersection(_:using:) 和朋友(iOS 16 中的新功能)坏了?

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

UIImageView 使用 CGAffineTransform 错误旋转

Xcode 8,iOS 10 -为进程启动 WebFilter 日志(log)记录

如何在通用应用程序中同时支持 iPad 和 iPhone 视网膜图形

当键盘出现在 iOS 中时,将 UIView 上移

iOS 中的 Crashlytics 不会继续通过 Fabric 应用程序中的构建您的项目

UIView 动画与 CALayers

如何隐藏uitabbar控制器

Swift 将 unix 时间转换为日期和时间

有没有办法在应用store 之外分发 ios 应用程序?

如何使用 AFNetworking 设置超时

UIView 中的 safeAreaInsets 在 iPhone X 上为 0

具有两种不同字体大小的 NSAttributedString 示例?