SwiftUI自定义TabBar:实现中间带弧形的底部导航栏
自定义带弧形底部的TabBar问题
我想将TabBar定制为中间带有弧形的样式,但目前仅能在中间添加一个图标。希望弧形边框位于圆形按钮下方,尝试了多种方法都未成功,希望能得到帮助。
当前实现效果

预期效果

TabBarView代码
struct TabBarView: View { @State var selectedTab = 0 var body: some View { ZStack(alignment: .bottom) { TabView(selection: $selectedTab) { HomeView() .tag(0) Search() .tag(1) Tickets() .tag(2) Profile() .tag(3) Settings() .tag(4) } RoundedRectangle(cornerRadius: 25) .frame(width: 350, height: 70) .foregroundColor(.white) .shadow(radius: 0.8) Button { selectedTab = 2 } label: { CustomTabItem(imageName: "ticket", title: "Ticket", isActive: (selectedTab == 2)) } .frame(width: 65, height: 65) .background(Color.white) .clipShape(Circle()) .shadow(radius: 0.8) .offset(y: -50) HStack { ForEach(TabbedItems.allCases, id: \.self) { item in if item != .ticket { // 排除中间按钮 Button { selectedTab = item.rawValue } label: { CustomTabItem(imageName: item.iconName, title: item.title, isActive: (selectedTab == item.rawValue)) } } } } .frame(height: 70) } } }
扩展代码
extension TabBarView { func CustomTabItem(imageName: String, title: String, isActive: Bool) -> some View{ HStack(alignment: .center,spacing: 22){ Spacer() Image(imageName) .resizable() .renderingMode(.template) .foregroundColor(isActive ? .purple : .gray) .frame(width: 25, height: 25) Spacer() } } }
内容的提问来源于stack exchange,提问作者Aruna
相关产品推荐
相关产品推荐

