在SwiftUI中增加标签栏项目图标的大小

时间:2019-09-03 21:48:23

标签: swiftui

是否可以增加标签栏项目图标的大小?
.frame(width: 40, height: 40)没有帮助。

Settings()
    .tabItem {
        VStack {
            Image(systemName: "archivebox")
        }
    }
    .tag(1)

4 个答案:

答案 0 :(得分:3)

您可以创建自定义tabView以获得自定义高度

此屏幕截图显示了受此Gist启发的自定义标签视图的结果

struct TabView: View {
    var views: [TabBarItem]
    @State var selectedIndex: Int = 0

    init(_ views: [TabBarItem]) {
        self.views = views
    }

    var body: some View {
        ZStack {
            ForEach(views.indices) { i in
                self.views[i].view
                    .opacity(self.selectedIndex == i ? 1 : 0)
            }
            GeometryReader { geometry in
                VStack {
                    Spacer()
                    ZStack(alignment: .top) {
                        LinearGradient(gradient: Gradient(colors: [Color.black.opacity(0.3), Color.black.opacity(0.4)]), startPoint: .top, endPoint: .bottom)
                            .frame(height: 70 + geometry.safeAreaInsets.bottom)

                        HStack {
                            ForEach(self.views.indices) { i in
                                Button(action: {
                                    self.selectedIndex = i
                                }) {
                                    VStack {
                                        if self.selectedIndex == i {
                                            self.views[i].image
                                                .foregroundColor(.white)
                                                 .padding(.top,10)
                                                .font(.title)
                                        } else {
                                            self.views[i].image
                                                .foregroundColor(Color.white.opacity(0.4))
                                                .padding(.top,10)
                                               .font(.title)
                                        }
                                        Text(self.views[i].title)
                                            .foregroundColor(.white)
                                            .font(Font.system(size: 16, weight: .bold))
                                            .padding(.top,10)
                                            .opacity(0.5)
                                    }
                                    .frame(maxWidth: .infinity)
                                }
                            }
                        }
                    }
                }
                .edgesIgnoringSafeArea(.bottom)
                .animation(.easeInOut)
            }
        }
    }
}

struct TabBarItem {
    var view: AnyView
    var image: Image
    var title: String

    init<V: View>(view: V, image: Image, title: String) {
        self.view = AnyView(view)
        self.image = image
        self.title = title
    }
}


/// Main View
struct ContentView: View {
  var body: some View {
    TabView([
      TabBarItem(view: Text("This is home screen"),
                 image: Image(systemName:"house.fill"),
                 title: "home"),
      TabBarItem(view: Text("2"),
                 image: Image(systemName:"heart.fill"),
                 title: "favorite"),

    ])
  }
}

enter image description here

答案 1 :(得分:2)

您可以使用.font()修饰符来放大图像:

YourView()
    .tabItem {
        Image(systemName: "…").font(.title)
    }

可悲的是,将图像向右推到标签栏顶部时,填充看起来不正确。

答案 2 :(得分:1)

您可以直接使用这样的字体大小(在Xcode 11.3中进行了测试):

YourView()
   .tabItem { 
      Image(systemName: "heart").font(.system(size: 26)) 
      Text("Offers") 
   }

答案 3 :(得分:-1)

没有,没有办法做到。所有修饰符都会被忽略。苹果文档说:

  

选项卡视图仅支持文本,图像或图像类型的选项卡项   其次是文字。传递任何其他类型的视图都会导致可见   但标签项为空。