swift - SwiftUI - 基于设备的自定义 TabBar 高度
问题描述
我正在尝试根据设备设置自定义 TabBar 的高度,我的代码:
struct MyTabBar: View {
@Binding var index: Int
var body: some View {
HStack {
Button(action: {
self.index = 0
}) {
Image(ImageText.iconHome.image)
}
Spacer(minLength: 0)
Button(action: {
self.index = 1
}) {
Image(ImageText.iconBell.image)
}
Spacer(minLength: 0)
Button(action: {
self.index = 2
}) {
Image(ImageText.iconAdd.image)
}
Spacer(minLength: 0)
Button(action: {
self.index = 3
}) {
Image(ImageText.iconSearch.image).foregroundColor(Color.red)
}
Spacer(minLength: 0)
Button(action: {
self.index = 4
}) {
Image(ImageText.iconHamburger.image)
}
}.padding(.horizontal, 26).frame(height: 56)
}
}
如果设备有缺口,如何将自定义 TabBar 设置为更高的高度?SwiftUI 是否有一些在这里有用的东西?
解决方案
您可以使用该GeometryReader
元素访问安全区域插图,并将其添加为填充使用.safeAreaInsets.bottom
(请注意,Xcode 自动完成几乎从不处理GeometryProxy
属性):
var body: some View {
GeometryReader { proxy in
VStack {
// Content goes here
Spacer()
// Custom tab bar
HStack {
Spacer()
Button(action: {}) {
Image(systemName: "house.fill")
.padding()
}
Spacer()
Button(action: {}) {
Image(systemName: "house.fill")
.padding()
}
Spacer()
Button(action: {}) {
Image(systemName: "house.fill")
.padding()
}
Spacer()
Button(action: {}) {
Image(systemName: "house.fill")
.padding()
}
Spacer()
}
.padding(.bottom, proxy.safeAreaInsets.bottom)
.background(Color.red)
}
}.edgesIgnoringSafeArea(.bottom)
}
推荐阅读
- c# - 如何从列表中的字典中设置/检索值?
- batch-file - 批处理文件执行后退出 CMD
- fortran - Fortran 代码中与 EOF 命令相关的错误
- python - 尝试使用 python(matplotlib 和 numpy)绘制函数,如果我稍微改变它,就会遇到值错误和类型错误
- python - 限制海龟中 pensize() 的最小值/最大值
- android - Android Studio 如何制作具有软透明背景的自定义按钮
- reverse-proxy - proxy_pass 和重定向有什么区别?
- google-maps - 是否可以为 GoogleMap 小部件添加不透明度?
- c# - 如何使用 Xamarin 实现我们在谷歌地图和其他应用程序中看到的地址栏
- c++ - C++ 中的 queue.front() 返回一个奇怪的值