如何在 UITabBarItem 中垂直居中 SF Symbols 图像? [英] How to center a SF Symbols image vertically in UITabBarItem?
问题描述
我在我的 iOS 应用程序中使用 SF 符号图像作为标签图像,方法如下:
I am using SF Symbols images as tab images in my iOS app by assigning them as follows:
self.tabBarItem.image = UIImage(systemName: "ellipsis")
这会导致所有图像顶部对齐,但我希望它们垂直居中.
This results in all images being top-aligned, but I would like to have them centered vertically.
我该怎么做?
推荐答案
显然 SF 符号默认以系统字体大小呈现.因此,如果您向省略号符号添加一半大小的基线偏移量,您几乎可以完全以这种方式将其垂直居中.
Apparently SF symbols are rendered with system font size by default. So if you add a baseline offset of half that size to the ellipsis symbol you could almost perfectly center it vertically that way.
这几乎是完美的,因为省略号有自己的高度,这个解决方案没有考虑到这个高度,即使它不多.
It's only almost perfect because ellipsis symbol has a height of its own which is not accounted for by this solution, even if it is not much.
self.tabBarItem.image = UIImage(systemName: "ellipsis")!.withBaselineOffset(fromBottom: UIFont.systemFontSize / 2)
这篇关于如何在 UITabBarItem 中垂直居中 SF Symbols 图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!