I have been given the following design for a tabbar:
I'm pretty close to replicating it, here is how my implementation looks:
Where I am struggling however is the finer detail of the alignment of the icons. So here is my code for the TabBarView:
struct TabBarView: View {
@ObservedObject var viewModel: TabBarViewModel
var body: some View {
HStack {
HStack(alignment: .bottom) {
tabOption(inactiveIcon: Image.Icons.Stores.notSelected, activeIcon: Image.Icons.Stores.selected, title: "Stores", tabNumber: 1, height: 20.8)
Spacer()
tabOption(inactiveIcon: Image.Icons.Menu.notSelected, activeIcon: Image.Icons.Menu.selected,title: "Menu", tabNumber: 2, height: 26)
Spacer()
tabOption(inactiveIcon: Image.Icons.Account.notSelected, activeIcon: Image.Icons.Account.selected,title: "Account", tabNumber: 3, height: 26)
Spacer()
tabOption(inactiveIcon: Image.Icons.Basket.notSelected, activeIcon: Image.Icons.Basket.selected,title: "Basket", tabNumber: 4, height: 23.11)
}
.frame(height: 39)
}
.frame(height: 64)
}
func tabOption(inactiveIcon: Image, activeIcon: Image, title: String, tabNumber: Int, height: CGFloat) -> some View {
Button {
viewModel.selectTab(tabNumber)
} label: {
VStack {
if viewModel.selectedTab == tabNumber {
activeIcon
.resizable()
.renderingMode(.template)
.foregroundColor(.snappyBlue)
.aspectRatio(contentMode: .fit)
.frame(height: height)
} else {
inactiveIcon
.resizable()
.renderingMode(.template)
.foregroundColor(.snappyBlue)
.aspectRatio(contentMode: .fit)
.frame(height: height)
}
Text(title)
.font(.Caption1.semiBold())
.foregroundColor(.black)
}
}
}
}
So each button or option is in a VStack, and I am then placing these buttons within an HStack. In fact, I have 2 HStacks, one embedded in the other (the outer one represents the entire tab bar area, the inner one contains the actual icons and has a set height to ensure that the labels align).
However, if you look closer at the design the icons are all slightly different heights. They are not aligned across the bottom, rather they are aligned vertically, i.e. the vertical centre of the stores icon is aligned with the vertical centre of the menu item. How can I go about aligning these as desired?
It feels right to have the buttons in separate VStacks, and the text needs to be aligned at the bottom, but it then seems difficult to align the images.
Here is how my solution looks in view hierarchy: